0

我正在尝试将模板加载到$templateCache模块的运行块中,如下所示。

angular.module('myapp').run(function($http, $templateCache, $timeout) {
    var templateLocation = 'location/to/template.html';
    $http.get(templateLocation).them(function(response) {
        $templateCache.put(templateLocation, response.data);
    )};
}

这会将模板加载到 templateCache 中。但是,当我尝试在指令中使用它时。它不会加载,因为指令在$http承诺得到解决之前加载。

这是指令的代码

angular.module('myApp').directive('myDirective, directiveFn);
directiveFn.$inject = ["$templateCache"]

function directiveFn($templateCache) {
    var templateLocation = 'location/to/template.html';
    return {
        restrict: 'EA'
        scope: {
            thing1: "="
        }
        template: $templateCache.get(templateLocation)
    }
}

有没有更好的方法/地方来做到这一点?

4

1 回答 1

1

$templateRequest在你的指令中使用怎么样?我在指令启动时使用它来加载模板,这允许我在指令中使用模板 HTML。

我正在使用 Typescript,而我的 $templateRequest 依赖于我的指令类,这就是它在 this 范围内的原因。 this.$templateRequest( 'client/lib/directives/aTemplate.html' ).then( ( html ) => { ... do something } );

快速浏览告诉我,您也可以将它与模板缓存一起使用。该线程讨论了其中的一些内容: https ://github.com/angular/angular.js/issues/10630

于 2016-04-09T00:24:42.347 回答