Reputation: 6053
I am using the ngcordova contacts plugin to retrieve the contacts in an app. I'd like to know if it is possible to get only the contacts that have at least one phone Number.
I have use the following code , It returns the my google contacts which contains email but not phone numbers. But I want only available phone number not emails. Is this possible ? or any other option available to get this result.
$scope.getContactList = function() {
$ionicLoading.show({
template: 'Loading...'
});
var options = {};
options.multiple = true;
options.hasPhoneNumber = true;
options.fields = ['name.formatted', 'phoneNumbers'];
$cordovaContacts.find(options).then(function(result) {
$scope.contacts = result;
$ionicLoading.hide();
}, function(error) {
console.log("ERROR: " + error);
});
}
Upvotes: 1
Views: 1953
Reputation: 6053
I got solution without using any external js , code shown as follows :
$scope.getContactList = function() {
$scope.contacts = [];
$ionicLoading.show({
template: 'Loading...'
});
var options = {};
options.multiple = true;
$cordovaContacts.find(options).then(function(result) {
for (var i = 0; i < result.length; i++) {
var contact = result[i];
if(contact.phoneNumbers != null)
$scope.contacts.push(contact);
}
$ionicLoading.hide();
}, function(error) {
console.log("ERROR: " + error);
});
}
Upvotes: 2
Reputation: 1496
I'd suggest using http://underscorejs.org/ in order to filter the contacts result. Something like this should suit your needs:
$scope.getContactList = function() {
$ionicLoading.show({
template: 'Loading...'
});
var options = {};
options.multiple = true;
options.hasPhoneNumber = true;
options.fields = ['name.formatted', 'phoneNumbers'];
$cordovaContacts.find(options).then(function(result) {
$scope.contacts = result;
var contactsWithAtLeastOnePhoneNumber = _.filter(result, function(contact){
return contact.phoneNumbers.length > 0
});
//
// Contacts with at least one phone number...
console.log(contactsWithAtLeastOnePhoneNumber);
$ionicLoading.hide();
}, function(error) {
console.log("ERROR: " + error);
});
}
Because the phoneNumbers
array can be returned and be blank, this quick method ensures that at least one entry is present.
Upvotes: 2