简体   繁体   English

在ngCordova Contacts插件中获取所有联系人至少一个电话号码

[英]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. 我正在使用ngcordova 联系人插件来检索应用程序中的联系人。 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. 我使用以下代码,它返回我的google联系人,其中包含电子邮件但不包含电话号码。 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. 我建议使用http://underscorejs.org/来过滤联系人结果。 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. 由于phoneNumbers数组可以返回并且为空,因此此快速方法可确保至少存在一个条目。

I got solution without using any external js , code shown as follows : 我得到了不使用任何外部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.

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