在离子应用程序中通过ngCordova获取和操作电话联系人



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

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

.HTML

<p style="white-space: pre;">{{contacts | json: 3}}</p>

但是angular.forEach不起作用,没有错误,怎么了?

终于解决了问题::

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

最新更新