Aki*_*len 3 angularjs ionic-framework
我正在尝试使用ngCordova模块中定义的$ cordovaContacts服务.我试图在服务中获取手机联系人,以便我可以跨控制器使用它.
Service.js
angular.module("services", ['ngCordova'])
.factory("ContactManager", function($cordovaContacts) {
var contacts; //variable that holds contacts, returned from getContacts
return {
getContacts: function() {
var options = {};
options.filter = "";
options.multiple = true;
//get the phone contacts
$cordovaContacts.find(options).then(function(result) {
contacts = result;
}, function(err) {
});
return contacts;
}
}
});
Run Code Online (Sandbox Code Playgroud)
Controller.js
angular.module("controllers", ['services'])
.controller("ContactCtrl", function(ContactManager) {
$scope.contacts = ContactManager.getContacts(); //this doesn't get set
});
Run Code Online (Sandbox Code Playgroud)
问题是'$ scope.contacts'没有在控制器内设置.但是,当直接将服务代码放在控制器内而不使用服务时,代码可以正常工作.我一直试图找出问题所在.请帮忙!
getContacts: function() {
var options = {};
options.filter = "";
options.multiple = true;
//get the phone contacts
$cordovaContacts.find(options).then(function(result) {
contacts = result;
}, function(err) {
});
return contacts;
}
Run Code Online (Sandbox Code Playgroud)
应该
getContacts: function() {
var options = {};
options.filter = "";
options.multiple = true;
//get the phone contacts
return $cordovaContacts.find(options);
}
Run Code Online (Sandbox Code Playgroud)
和控制器
$scope.contacts = ContactManager.getContacts().then(function(_result){
$scope.contacts = _result;
}, function(_error){
console.log(_error);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3034 次 |
| 最近记录: |