正如标题已经说过的,我想在指令模板函数中返回一个承诺,即:
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
}
};
});
由于模板属性不支持此功能,因此是否有任何(简单的)方法来"模拟"它?
看起来指令解析是我最好的选择,但我很难想出一种很好的方法来应用从 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'
};
});
在解析中返回模板,请按照 dan wahlin 的动态加载控制器示例进行操作,并为您的指令执行相同的 routeResolver.resolve 函数
http://weblogs.asp.net/dwahlin/archive/2013/05/22/dynamically-loading-controllers-and-views-with-angularjs-and-requirejs.aspx
如果它不起作用,请告诉我,我将创建 plunker 并更新它