正如标题所说,我想在指令模板函数中返回一个 promise ,即:
angular.module('someModule', [])
//...
.directive('someDirective', function() {
return {
//...
restrict: 'E',
template: function() {
var deferred = $q.defer();
//Some Async function which will call deferred.resolve
return deferred.promise; //Promise not supported by template property
}
};
});
由于模板属性不支持这个,有什么(简单的)方法可以“模拟”那个吗?
看起来指令 resolve 是我最好的选择,但我很难想出一个很好的方法来在 resolve 方法中应用从 WebSocket 加载的模板。
我的目标是对所有通信使用单个 WebSocket 连接。
最佳答案
您可以尝试另一种方法:
app.run(function($templateCache, myTemplateLoader) {
myTemplateLoader.load('templateName').then(function(content) {
$templateCache.put('templateName', content);
});
});
现在您可以在指令中简单地使用 templateUrl
属性:
app.directive('someDirective', function() {
return {
// ...
templateUrl: 'templateName'
};
});
关于javascript - AngularJS:在指令模板函数中返回一个 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22189298/