Angularjs 通过Ionic应用程序中的ngCordova获取和处理电话联系人
我想得到所有的电话联系方式,并操纵他们发送给服务器。像这样Angularjs 通过Ionic应用程序中的ngCordova获取和处理电话联系人,angularjs,ionic-framework,ngcordova,Angularjs,Ionic Framework,Ngcordova,我想得到所有的电话联系方式,并操纵他们发送给服务器。像这样 $scope.contacts = []; $cordovaContacts.find({ filter: '', fields: ['displayName','phoneNumbers'] }).then(function (allContacts) { angular.forEach(allContacts, function (contact, index) { $sco
$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>
{{{contacts}json:3}
但是
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
});
}
}
});
});
您确定
allContacts
不为空吗?是的,因为当我使用$scope.contacts=allContacts
时,一切都正常。