我可以在ng-app之外注册ng-template

时间:2016-08-17 16:57:59

标签: javascript angularjs

无论如何以编程方式将ng-template添加到$ 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>

JS

var MyCtrl = blah;

var myDirective = function() {
    return {
        restrict: 'E',
        scope: {},
        templateUrl: 'myTemplate.html'
    }
};

/* Register templates here??? */

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

angular.bootstrap(document.getElementById('appContainer'), ['myApp']);

由于模板不在ng-app下,因此angular不知道它存在。显而易见的是不使用angular.bootstrap,并将ng-app放在主体上,但我正在尝试设计这个模块,因此它可以嵌套在现有的ng-app中,或者单独使用。当它在现有的ng-app中使用时,angular知道它,但当它独自时,angular不知道指令模板。

1 个答案:

答案 0 :(得分:1)

创建模块后,在运行功能中,您可以注册如下模板:

{{1}}