gpt4 book ai didi

javascript - 将带有 templateUrl 的指令编译为字符串并等待延迟链接函数

转载 作者:行者123 更新时间:2023-11-28 07:23:28 25 4
gpt4 key购买 nike

我想将一段包含多个指令的 html $compile 转换为字符串并将其发送到服务器。由于某些指令包含 templateUrl,因此返回的链接函数会延迟。在这个问题( AngularJS: Using $compile on html that contains directives with templateurl )中,问题似乎通过调用 $scope.$digest() 得到了解决,这在我的情况下是不可能的(已经在进行中)。

我找到的唯一解决方案是使用超时,请参阅 http://plnkr.co/edit/k3ZAYy1FhGUFXxXyrPvB?p=preview

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

最佳答案

在你的 runURL 函数中,使用 $apply ,例如:

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;
};

这可以将范围与模板绑定(bind)。请查看已编辑的plunk.

关于javascript - 将带有 templateUrl 的指令编译为字符串并等待延迟链接函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30027352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com