我是编写服务的新手。我正在我的工厂调用一个调用一系列操作的方法。
1) 从数据库中获取数据 2) 等待 $promise resolve 然后比较 & 过滤结果 3) 返回新的过滤列表
当我注销 $scope.new_list 时,我得到的只是一个空对象。有人可以帮我解决我做错的事吗?
people.factory('uniqueContacts', ['apiResource', function(apiResource) {
function getDB() {
return apiResource.query({api_resource:'people'})
}
function generateList(newlistdata){
getDB().$promise.then(function(result){
newlist = _.difference(newlistdata, result);
})
return new_list;
});
}
return{
buildList: function(newlistdata){
return generateList(newlistdata);
}
}
}]);
//in my controller
$scope.new_list = uniqueContacts.buildList(newlistdata);
console.log($scope.new_list) // undefined
最佳答案
您的服务函数应从成功返回 new_list
getDB().$promise.then(function(result){
newlist = _.difference(newlistdata, result);
return new_list;
})
});
Controller
uniqueContacts.buildList(newlistdata).then(function(new_list){
$scope.new_list = new_list;
});
关于angularjs - 在服务中返回已解析的 $resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30489805/