1

我想$compile一段包含几个指令的html到一个字符串并发送给服务器。由于某些指令包含templateUrl,所以返回的链接函数被延迟。在这个问题中(AngularJS: Using $compile on html that contains directives with templateurl)这个问题似乎解决了,呼吁$scope.$digest()这在我的情况下是不可能的(已经在进行中)。用templateUrl编译指令到字符串并等待延迟的链接功能

我发现的唯一的解决办法,是使用超时,请参阅http://plnkr.co/edit/k3ZAYy1FhGUFXxXyrPvB?p=preview

有没有更好的方式来等待延迟链接功能的完成?

在runURL功能

回答

0

,使用$应用,如:

this.runUrl = function() { 
    var scope = $rootScope.$new(); 
    var html = '<template-url-directive></template-url-directive>'; 
    var compiled = $compile(html); 
    var el = compiled(scope); 
    scope.$apply(); 
    this.result = el[0].outerHTML; 
    }; 

,致力于与模板范围绑定。请检查编辑的plunk.

+0

我得到'错误:[$ rootScope:inprog] $ apply already progress in progress'。我认为它的工作原理只是因为apply()中的错误需要足够的时间来加载模板。 – hansmaad

+0

我编辑过你的重拳,请检查它的新叉。 –

+0

你的摔跤叉同样的错误。 – hansmaad