angularjsangularjs-ng-template

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.


Solution

  • 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
    }