从文件加载$ templateCache

Kom*_*omo 4 templates angularjs

我一直无法从$ 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});
Run Code Online (Sandbox Code Playgroud)

我如何加载它们:

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'
};
}]);
Run Code Online (Sandbox Code Playgroud)

在我的网络浏览器选项卡中,我可以看到在页面加载时加载的模板.

但是,在调用我的指令时出现以下错误:

One of template or templateUrl options is required.
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么 ?

谢谢

Dot*_*Bot 9

对这个旧线程的微小补充:尽管实现目标的方法很少,但我发现对我来说最好的方法是将$templateCache逻辑添加到我的应用程序的每个指令中.这样我可以避免使用任何外部软件包grunt angular-templates,这很棒 - 但对我的应用程序来说有点过分.

angular.module('MyApp')
.directive('MyDirective', ['$templateCache', 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);
    })
});
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!