[英]angular restangular service is not working correctly
I have a page which has ng-init = find(), after I go to other pages and do something with the db the data in the original page will change. 我有一个具有ng-init = find()的页面,在我转到其他页面并对数据库执行某些操作后,原始页面中的数据将发生变化。 And then I redirect the user to the original page.
然后,我将用户重定向到原始页面。 And I expect to see changes in the first page.
我希望第一页会看到更改。
$scope.find = function() {
Stats.query
.then(function(stats){
$scope.stats = stats;
});
};
where the Stats is a service I created with restangular injected. Stats是我使用矩形插入创建的服务。 The service looks like this:
该服务如下所示:
angular.module('mean.stats').factory('Stats', ['Restangular',
function(Restangular) {
var stats = Restangular.all('stats');
return {
query: stats.getList()
}
}]);
It doesn't work as I expected. 它没有按我预期的那样工作。 But if I use the traditional $resource approach the outcome is correct.
但是,如果我使用传统的$ resource方法,则结果是正确的。
angular.module('mean.stats').factory('Stats', ['$resource',
function($resource) {
return $resource('stats/:statsId', {
statsId: '@_id'
});
}]);
Also later I have figured out this restangular thing is also working correctly. 同样后来,我发现这种矩形关系也可以正常工作。 I don't quite understand why is that... Does anyone know what is happening?
我不太明白为什么会这样...有人知道发生了什么吗? Thanks.
谢谢。
angular.module('mean.stats').factory('Stats', ['Restangular',
function(Restangular) {
var stats = Restangular.all('stats');
return {
query: function(){
return stats.customGETLIST('');
}
}
}]);
In your second code block, you assigned Stat.query
to a promise returned by getList()
instead of a function invoking getList()
. 在第二个代码块中,您将
Stat.query
分配给了getList()
返回的getList()
而不是调用getList()
的函数。 So the request was sent to server only once when the Stat
factory was defined. 因此,在定义
Stat
工厂时,该请求仅发送到服务器一次。 To make it work, first make Stat.query
a function invoking getList()
: 为了使其工作,首先使
Stat.query
一个调用getList()
的函数:
angular.module('mean.stats').factory('Stats', ['Restangular',
function(Restangular) {
var stats = Restangular.all('stats');
return {
query: function() {
return stats.getList();
}
}
}]);
Then invoke Stat.query
in your $scope.find
method: (add ()
after the query
): 然后在
$scope.find
方法中调用Stat.query
在query
之后添加()
):
$scope.find = function() {
Stats.query()
.then(function(stats){
$scope.stats = stats;
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.