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

Get and manipulate phone contacts by ngCordova in Ionic app

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

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

           }

        }

    });    

});