您好我不明白爲什麼總是我從這項服務中獲得空數組時,我的控制器返回值從角服務
angular
.module('dimecuba.services', [])
.factory('Contacts', function($cordovaContacts, $ionicPlatform) {
var contactsFound = [];
var contacts = {
all:function(){
var options = {};
options.multiple = true;
options.filter = "";
//options.fields = ['displayName'];
options.hasPhoneNumber = true;
$ionicPlatform.ready(function(){
$cordovaContacts.find(options).then(
function(allContacts){
angular.forEach(allContacts, function(contact, key) {
contactsFound.push(contact);
});
console.log("Contacts Found:" + JSON.stringify(contactsFound));
return contactsFound;
},
function(contactError){
console.log('Error');
}
);
});
}
}; //end contacts
console.log("Contacts:"+JSON.stringify(contacts));
return contacts;
});
顯示$ cordovaContacts和$ ionicPlatform包含的內容。你注射這些服務? –
目標是return contactsFound ...我可以如何修復此代碼返回contacstFound – icemandev
看起來您正在返回「contacts」而不是'contactsFound'。在'contacts'中聲明'contactsFound'並再次嘗試 –