Contacts

在设备上创建、删除和搜索通讯录。

代码实例

1.module.controller('MyCtrl', function($scope, $cordovaContacts, $ionicPlatform) {
2. $scope.addContact = function() {
3. $cordovaContacts.save($scope.contactForm).then(function(result) {
4. // Contact saved
5. }, function(err) {
6. // Contact error
7. });
8. };
9.
10. $scope.getAllContacts = function() {
11. $cordovaContacts.find().then(function(allContacts) { //omitting parameter to .find() causes all contacts to be returned
12. $scope.contacts = allContacts;
13. }
14. };
15.
16. $scope.findContactsBySearchTerm = function (searchTerm) {
17. var opts = { //search options
18. filter : searchTerm, // 'Bob'
19. multiple: true, // Yes, return any contact that matches criteria
20. fields: [ 'displayName', 'name' ] // These are the fields to search for 'bob'.
21. desiredFields: [id]; //return fields.
22. };
23.
24. if ($ionicPlatform.isAndroid()) {
25. opts.hasPhoneNumber = true; //hasPhoneNumber only works for android.
26. };
27.
28. $cordovaContacts.find(opts).then(function (contactsFound) {
29. $scope.contacts = contactsFound;
30. };
31. }
32.
33. $scope.pickContactUsingNativeUI = function () {
34. $cordovaContacts.pickContact().then(function (contactPicked) {
35. $scope.contact = contactPicked;
36. }
37. }
38.
39.});