Javascript 我可以从ng应用程序外部注册ng模板吗

Javascript 我可以从ng应用程序外部注册ng模板吗,javascript,angularjs,Javascript,Angularjs,是否仍然可以通过编程方式将ng模板添加到$templateCache HTML <script type="text/ng-template" id="myTemplate.html"> I'm a template </script> <div id="appContainer" ng-controller="MyCtrl"> <my-directive></my-directive> </div>

是否仍然可以通过编程方式将ng模板添加到$templateCache

HTML

<script type="text/ng-template" id="myTemplate.html">
    I'm a template
</script>

<div id="appContainer" ng-controller="MyCtrl">
    <my-directive></my-directive>   
</div>

由于模板不在ng应用程序下,angular不知道它存在。最明显的是不要使用angular.bootstrap,并将ng应用程序放在主体上,但我正在尝试设计此模块,以便它可以嵌套在现有ng应用程序中,或单独使用。在现有ng应用程序中使用angular时,angular知道它,但单独使用angular时,angular不知道指令模板。

创建模块后,可以在run函数中注册如下模板:

 angular.module('myApp', [])
   .controller('MyCtrl', MyCtrl)
   .directive('myDirective', myDirective)

 angular.module('myApp')
  .run(['$templateCache', function($templateCache) {
    $templateCache.put('myTemplate.html',
      'I\'m a template'
    );
  });
 angular.module('myApp', [])
   .controller('MyCtrl', MyCtrl)
   .directive('myDirective', myDirective)

 angular.module('myApp')
  .run(['$templateCache', function($templateCache) {
    $templateCache.put('myTemplate.html',
      'I\'m a template'
    );
  });