AngularJS:在指令模板函数中返回promise [英] AngularJS: Returning a promise in directive template function
问题描述
正如标题已经说过的,我想在指令模板函数中返回一个promise,即:
As the title already says, I'd like to return a promise in the directive template function i.e.:
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
}
};
});
由于template属性不支持此功能,因此有没有(简单的)模仿"方式?
Since the template property doesn't support this, is there any (simple) way to "emulate" that?
看来,指令解析是我最好的选择,但是我很难想出一种很好的方法来在resolve方法中应用从WebSocket加载的模板.
It looks like directive resolve is my best bet, but I'm having a hard time coming up with a nice way to apply the template loaded from a WebSocket in the resolve method.
我的目标是使用单个WebSocket连接进行所有通信.
My goal is to use a single WebSocket connection for all communication.
推荐答案
您可以尝试另一种方法:
You could try another approach:
app.run(function($templateCache, myTemplateLoader) {
myTemplateLoader.load('templateName').then(function(content) {
$templateCache.put('templateName', content);
});
});
现在您可以在指令中简单地使用templateUrl
属性:
And now you can simply use the templateUrl
property in your directives:
app.directive('someDirective', function() {
return {
// ...
templateUrl: 'templateName'
};
});
这篇关于AngularJS:在指令模板函数中返回promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!