我一直无法从$templateCache加载模板.
我如何在$templateCache中放置模板:
var app = angular.module('anglober',['anglober.controllers','anglober.services','anglober.directives']).run(function ($templateCache,$http) { $http.get('anglober/js/invitation/invitationModal.tpl.html',{cache: $templateCache}); $http.get('anglober/js/modal/ajaxModal.tpl.html',{cache: $templateCache}); $http.get('anglober/js/ajaxLoader/ajaxLoader.tpl.html',{cache: $templateCache}); $http.get('anglober/js/modal/modalContent.tpl.html',{cache: $templateCache}); $http.get('anglober/js/modal/simpleModal.tpl.html',{cache: $templateCache}); $http.get('anglober/js/mog/topMogs.tpl.html',{cache: $templateCache});
我如何加载它们:
angular.module('anglober.directives').directive('topMogs',['$templateCache',function ($templateCache) { return { restrict : 'E',template: $templateCache.get('topMogs.tpl.html') //Tried this too // templateUrl: 'topMogs.tpl.html' }; }]);
在我的网络浏览器选项卡中,我可以看到在页面加载时加载的模板.
One of template or templateUrl options is required.
我究竟做错了什么 ?
谢谢
解决方法
对这个旧线程的微小补充:尽管实现目标的方法很少,但我发现对我来说最好的方法是将$templateCache逻辑添加到我的每个应用程序的指令中.这样我就可以避免使用任何外部软件包,比如grunt angular-templates,这很棒 – 但对我的应用来说有点过分.
angular.module('MyApp') .directive('MyDirective',function($templateCache) { return { restrict: 'E',template: $templateCache.get('MyTemplate'),controller: 'MyController',controllerAs: 'MyController' }; }]).run(function($templateCache,$http) { $http.get('templates/MyTemplate.html').then(function(response) { $templateCache.put('MyTemplate',response.data); }) });
希望这可以帮助!