简体   繁体   中英

Get all contacts at least one phone number in ngCordova Contacts plugin

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM