gpt4 book ai didi

javascript - 如何从回调函数中获取变量的值?

转载 作者:行者123 更新时间:2023-11-28 08:17:54 26 4
gpt4 key购买 nike

我想从回调函数中获取联系人值。

如何拥有我的 this.recherche = 联系人

App.provider('ContactsP', function() {

this.$get = function() {
return function(search) {

console.log('Factory ContactsCtrl RECHERCHE : ' + search);

this.recherche = [];
this.options = new ContactFindOptions();
this.options.filter = search;
this.options.multiple = true;
this.fields = ["displayName", "name", "phoneNumbers"];

navigator.contacts.find(this.fields, function(contacts) {

//I want to put contacts into this.recherche but that doesn't work....

this.recherche = contacts; return contacts;

}, function(e) {
console.log("Error finding contacts " + e.code)
}, this.options);

};
};

最佳答案

问题是,当您处于回调中时,您对 this 的引用不是您的封闭对象。您需要使用angular.bind传递对象的上下文。

navigator.contacts.find(this.fields, angular.bind(this, function(contacts) {

//I want to put contacts into this.recherche but that doesn't work....

this.recherche = contacts; return contacts;

}), function(e) {
console.log("Error finding contacts " + e.code)
}, this.options);

关于javascript - 如何从回调函数中获取变量的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23385760/

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