我正在尝试将模板加载到$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)
}
}
有没有更好的方法/地方来做到这一点?