angularjs – 从文件加载$templateCache

前端之家收集整理的这篇文章主要介绍了angularjs – 从文件加载$templateCache前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直无法从$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);
    })
});

希望这可以帮助!

猜你在找的Angularjs相关文章