簡體   English   中英

如何從回調函數獲取變量的值?

[英]How to get the value of a variable from a callback function?

我想從我的回調函數中獲取聯系人的值。

如何擁有我的this.recherche = contacts

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM