gpt4 book ai didi

javascript - 将参数传递给 AngularJS $timeout

转载 作者:数据小太阳 更新时间:2023-10-29 05:52:02 26 4
gpt4 key购买 nike

我正在开发一个 ionic 移动应用程序,我需要将参数传递给 $timeout promise ,以便我可以使用该参数进行一些操作。我阅读了有关 $timeout (https://docs.angularjs.org/api/ng/service/$timeout) 的 angularjs 文档,它说最后一个参数可以是传递给超时函数的参数。我试过这个:

$timeout(function(params){
alert(params.p1 + " - " + params.p2);
}, 5000, true, {p1 : "Hello", p2 : "World"});

但它不起作用,我无法访问超时函数内的 params 变量。

我做错了什么吗?或者,还有其他方法吗?

谢谢

最佳答案

这是一个新论点introducedangular 1.4.x .因此,您可能正在尝试将其与 angular 1.3 or lesser version 一起使用.

Example

只要您使用的是正确版本的 angular,您的示例就应该可以正常工作。

 $timeout(function(p){
console.log(p.a);
},0, true, {a:1, b:2});

另一件需要注意的事情是你没有将它作为参数传递给超时 promise ,它们只是传递给 $timeout 服务运行的函数的参数。如果您想将参数作为由超时 promise 解析的值传递,则只需返回该值即可。

 $timeout(function(p){
return p; //<-- return it
},0, true, {a:1, b:2})
.then(function(value){
console.log(value)//<-- this is same as p
});

如果您的真正意图是将参数传递给版本 < 1.4 的函数,则只需将其移至函数并调用它:

 function callIt(params){
return $timeout(function(){ //Return promise if needed
//Access params here from the outer closure of the function.
})
}

然后调用:

callIt({a:1, b:2});

关于javascript - 将参数传递给 AngularJS $timeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30447019/

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