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. 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. 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.
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()
. So the request was sent to server only once when the Stat
factory was defined. To make it work, first make Stat.query
a function invoking 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 = function() {
Stats.query()
.then(function(stats){
$scope.stats = stats;
});
};