[英]Factory is returning a function instead of the return value of the function in AngularJS
So, I had an application pretty much figured out until I realized that I am making way to many calls to database api and as a result the app is lagging. 因此,我有了一个几乎可以解决的应用程序,直到我意识到我正在让许多对数据库api的调用让步,结果该应用程序滞后了。 I am trying to set up a way to keep the current scope accurate when adding and removing items from the $scope object instead of having to request the entire list from the database every time.
我试图建立一种在$ scope对象中添加和删除项目时保持当前范围准确的方法,而不是每次都必须从数据库中请求整个列表。 So I figured from all I've read that a factory is the way to go.
因此,从我读过的所有文章中我都知道,要走工厂是路要走。 That way I can pull the data from the database once, then after successfully adding or deleting entries through the api I can just manipulate the current scope instead of requesting the entire list again.
这样,我可以一次从数据库中提取数据,然后通过api成功添加或删除条目后,我可以操纵当前作用域,而不必再次请求整个列表。 My question is (with an example), How do I fetch a list of users from the api and add that to the scope.
我的问题是(带有示例)如何从api获取用户列表并将其添加到范围。 Then when I add or delete users from the list I can just remove them from the scope without fetching the list all over again from the api?
然后,当我从列表中添加或删除用户时,我可以将他们从范围中删除,而无需从api再次获取列表?
Here is my factory: 这是我的工厂:
app.factory("UsersFactory",['Data',function(Data){
var users;
function init(){
Data.get('users')
.then(function (results) {
users = results;
});
}
init();
function getUsers(){
return users;
}
return {
getUsers:getUsers
}
}]);
And after injecting the factory in the controller: 并将工厂注入控制器后:
console.log(UsersFactory.getUsers);
This returns the function but not the return value of the function. 这将返回函数,但不会返回函数的返回值。
I have also tried: 我也尝试过:
console.log(UsersFactory.getUsers());
Which returns undefined; 返回未定义;
I would actually like to do all this in the resolve for the state (ui-router) but in the controller directly is ok too i guess. 我实际上想在解析状态(ui路由器)中完成所有这些操作,但是我想直接在控制器中也可以。
Any help would be ap;appreciated and feel free to ask me to clarify anything. 任何帮助将不胜感激,并随时要求我澄清任何事情。 I'm not very good at clearly asking questions.
我不太擅长清楚地提出问题。
The correct way to do this is to return a promise from your factory
: 正确的方法是从
factory
退还承诺:
app.factory("UsersFactory", ['Data', function(Data) {
var factory = {
getUsers: getUsers
};
return factory;
function getUsers() {
return Data.get('users');
}
}]);
Then, in your controller
: 然后,在您的
controller
:
app.controller("UsersController", ['$scope', 'UsersFactory', function($scope, UsersFactory) {
function getResponse(response) {
$scope.users = response.data;
}
function getError(response) {
console.log(response);
}
UsersFactory.getUsers()
.then(getResponse);
.catch(getError);
}]);
Factory: 厂:
app.factory("UsersFactory",function(Data) {
UpDateUserinfo:function(){
var inf;
inf=Data.get('users');
return inf;
}
})
Controller: 控制器:
app.controller("UsersController", function($scope, UsersFactory){
UsersFactory.UpDateUserinfo().$promise.then(function(data){
$scope.users=data
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.