gpt4 book ai didi

angularjs - 如何在 Angular 中将 Promise 作为指令属性传递

转载 作者:行者123 更新时间:2023-12-02 19:18:52 25 4
gpt4 key购买 nike

我想设置一个 Promise,然后将其放入指令中(使用数据绑定(bind)“=”属性类型)并在指令中执行 then 魔法,但将 Promise 传递为属性似乎导致 promise 被解析为未定义,因此then(以及任何其他 promise 功能)不可用。

最佳答案

我怀疑这是由于这个特定的调整造成的:

$q promises are recognized by the templating engine in angular, which means that in templates you can treat promises attached to a scope as if they were the resulting values - angular $q docs

这本质上会导致 Promise 在模板内转换为 Promise 用于存储结果值的 $$v 值,并且在链接阶段这可能仍然是未定义的- 然后将转换后的值(即未定义)而不是 promise 本身传递到指令中。

解决方法是不使用 Promise 本身作为传递给指令的属性,而是使其成为父对象的一部分:父对象不会有 $q/template 引擎魔术运行,因此您将能够深入了解父级的 promise 并从那里访问then功能。

参见http://jsfiddle.net/cebjyre/95sjT/举个例子 - 取消注释第 32 行将导致失败,因为此时顶级 promise 实际上不再是 promise ,而第 17 行的第二级 promise 工作正常

关于angularjs - 如何在 Angular 中将 Promise 作为指令属性传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17159614/

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