[英]Get all contacts at least one phone number in ngCordova Contacts plugin
我正在使用ngcordova 聯系人插件來檢索應用程序中的聯系人。 我想知道是否可以僅獲取至少具有一個電話號碼的聯系人。
我使用以下代碼,它返回我的google聯系人,其中包含電子郵件但不包含電話號碼。 但是我只希望可用的電話號碼而不是電子郵件。 這可能嗎 ? 或任何其他可獲得此結果的選項。
$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);
});
}
我建議使用http://underscorejs.org/來過濾聯系人結果。 這樣的事情應該適合您的需求:
$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);
});
}
由於phoneNumbers
數組可以返回並且為空,因此此快速方法可確保至少存在一個條目。
我得到了不使用任何外部js的解決方案,代碼如下所示:
$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);
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.