检查是否存在"text/ng-template"

And*_*erj 3 angularjs angularjs-ng-template

我正在编写一个小工具,我需要检查是否定义了某个ng-template.我的所有模板定义如下:

<script type="text/ng-template" id="example.html">...</script>
Run Code Online (Sandbox Code Playgroud)

所以通过$ http检查文件是否存在对我来说不起作用.如何检查所述模板是否已定义?我到目前为止看到的唯一选择是检查DOM:

if(angular.element.find('script#example.html').length > 0) { ...
Run Code Online (Sandbox Code Playgroud)

...但我真的很喜欢一个不需要检查DOM的更好的解决方案.

dfs*_*fsq 8

Script指令将模板内容放在$ templateCache(source ref.)中,如果它是模板的话.这意味着您应该能够通过检查模板在缓存中的存在来检查模板是否存在:

if ($templateCache.get('example.html')) {
    // template is on the page
}
Run Code Online (Sandbox Code Playgroud)