加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 从文件加载$templateCache

发布时间:2020-12-17 17:53:28 所属栏目:安全 来源:网络整理
导读:我一直无法从$templateCache加载模板. 我如何在$templateCache中放置模板: var app = angular.module('anglober',['anglober.controllers','anglober.services','anglober.directives']).run(function ($templateCache,$http) { $http.get('anglober/js/inv
我一直无法从$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);
    })
});

希望这可以帮助!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读