我刚开始使用Ionic Framework构建应用程序。但它基本上使用Angular.js所以问题应该很容易回答:)
挑战:我有一个虚拟联系人列表,我可以从ContactService获取。现在我想获得手机的实际联系人。问题是,我必须等待应用程序加载才能访问联系人。所以我必须将它包装在事件监听器中。这是我的代码,我怎样才能使它工作(成功函数获取一系列联系人,我检查了一下)?
document.addEventListener('deviceready', function() {
StatusBar.styleDefault();
function onSuccess(contacts) {
ContactService.add(contacts);
};
function onError(contactError) {
alert('onError!');
};
var options = new ContactFindOptions();
options.multiple = true;
var fields = ['displayName', 'name'];
navigator.contacts.find(fields, onSuccess, onError, options);
}, false);
angular.module('starter.services', [])
.factory('ContactService', function() {
var contacts = [];
return {
add: function(result) {
contacts = result;
},
all: function() {
return contacts;
},
get: function(contactId) {
return contacts[contactId];
},
count: function() {
return contacts.length;
}
}
});