Download $ templateCache from file

I'm having trouble loading templates from the $ templateCache template.

How I put templates in $ 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});

How do I download them:

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'
};
}]);

In my network browser tab, I see that the templates load when the page loads.

However, when I call my directive, I get the following error:

One of template or templateUrl options is required.

What am I doing wrong?

thank

+4
source share
1 answer

: , , , $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);
    })
});

, !

+7

Source: https://habr.com/ru/post/1568975/


All Articles