在Ionic应用程序中通过ngCordova获取和操作手机联系人

时间:2017-02-02 15:41:12

标签: angularjs ionic-framework ngcordova

我希望通过$cordovaContacts获取所有电话联系人并操纵它们以发送给服务器。像这样

<base href="/" />

HTML

$scope.contacts = [];

$cordovaContacts.find({
      filter: '', 
      fields: ['displayName','phoneNumbers']
}).then(function (allContacts) {

    angular.forEach(allContacts, function (contact, index) {
          $scope.contacts.push({
              "first_name": contact.name.givenName,
              "last_name": contact.name.familyName,
              "phone_number": contact.phoneNumbers[0].value
          });
    });
});

但是<p style="white-space: pre;">{{contacts | json: 3}}</p> 没有工作且没有错误,错了什么?

1 个答案:

答案 0 :(得分:0)

最后解决问题::

$cordovaContacts.find({
      filter: '', 
      fields: ['displayName', 'name', 'phoneNumbers']
}).then(function (allContacts) {

    var contacts = [];

    angular.forEach(allContacts, function (contact, index) {

       if (contact.phoneNumbers != null && 
           contact.phoneNumbers[0].type == 'mobile' &&
           contact.name != null) {

           // if user have firstName and lastName
           if (contact.name.givenName && contact.name.familyName) {

             contacts.push({
                "first_name": contact.name.givenName,
                "last_name": contact.name.familyName,
                "phone_number": contact.phoneNumbers[0].value
             });

           } else {

             contacts.push({
                "first_name": contact.name.givenName ? contact.name.givenName : '',
                "last_name": contact.name.familyName ? contact.name.familyName : '',
                "phone_number": contact.phoneNumbers[0].value
             });

           }

        }

    });    

});