AngularJS:在指令模板函数中返回promise

阿罗克

就像标题已经说过的那样,我想在指令模板函数中返回一个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
      }
    };
  });

由于template属性不支持此功能,是否有任何(简单)方式可以“模拟”呢?

看起来指令解析是我最好的选择,但是我很难想出一种很好的方法来在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'
    };
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章