简体   繁体   中英

check if “text/ng-template” exists

I am writing a little tool for which i need to check if a certain ng-template is defined. All my templates are defined like this:

<script type="text/ng-template" id="example.html">...</script>

So checking if the file exists via $http won't work for me. How can i check if said template was defined? The only option i see so far is checking the DOM:

if(angular.element.find('script#example.html').length > 0) { ...

... but i'd realy love a better solution that does not need to check the DOM.

Script directive puts template content in $templateCache (source ref. ) if it's a template. It means that you should be able to check if template exists by checking its presence in the cache:

if ($templateCache.get('example.html')) {
    // template is on the page
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM