获取cordova的联系人列表?

时间:2015-03-06 23:10:41

标签: angularjs cordova

抱歉我的英语不好。

如何使用Cordova angularjs获取联系人列表?

提前致谢。亲切的问候。

1 个答案:

答案 0 :(得分:1)

我不知道你的cordova-app是如何构建的,但是你可以这样做(考虑到我没有测试过这个):

代码:

  1. 首先使用cordova的condact-plugin请求设备的联系人: (与前面提供的链接相同:http://docs.phonegap.com/en/edge/cordova_contacts_contacts.md.html

    document.addEventListener("deviceready", onDeviceReady, false);
    
    function onDeviceReady() {
    
       // when using the plugin:
       // you can put it within your angularjs-controllers 
       // where it will be executed and onSuccess-callback is called.
       var options = new ContactFindOptions();
       options.filter = "";
       options.multiple=true; 
       var fields = ["displayName", "name"];
    
       navigator.contacts.find(fields, onSuccess, onError, options);
    
    }
    
    // within this function you have to assign contacts to a model
    function onSuccess(contacts) {
       $scope.contacts = contacts;
    }
    
    
    function onError(contactError) {
       alert('onError!');
    }
    
  2. HTML:

    1. 迭代onSuccess-function中指定的contacts-collection的每个contact-object:

      <div ng-repeat="contact in contacts">{{contact.name.formatted}}</div>
      
    2. 教程:http://www.quora.com/What-is-the-way-to-get-all-contacts-using-PhoneGap-on-Android

      将插件放入angularjs-controller中可能如下所示:

      angular.module('aModule', [])
      
      .controller('contactCtrl', ['$scope', function($scope) {
      
              var options = new ContactFindOptions();
              options.multiple = true;
              options.filter = "";
              var fields = ["displayName", "name"];
      
              navigator.contacts.find(fields, 
                  function(contacts){ 
      
                      var arr = [];
                      for (var i = 0; i < contacts.length; i++)
                      {
                        arr.push({name: contacts[i].name.formatted})
                      }
                      $scope.contacts = arr;
      
                  }, 
                  function(error){ console.log(error); },
                  options
              );
      
      }])
      

      HTML:

      <div ng-app="aModule" ng-controller="contactCtrl">
          <div ng-repeat="contact in contacts">{{contact.name}}</div>
      </div>