在我更新了angularjs中的用戶之後,我將使用新的userdata更新$ scope.user。一種方法是做一個新的API請求來獲取所有用戶。但我認爲,更好地更新$範圍。我發現$適用,但我不知道如何使用它。
app.controller('UserCtrl', function($scope, APIService) {
$scope.users = APIService.query({ route:'users' });
$scope.activate = function(user) {
var updateUser = {};
if(user.activated) {
updateUser.activated = false;
} else {
updateUser.activated = true;
}
APIService.update({ route:'users', id: user._id }, updateUser, function(updatedUser) {
//update $scope.users with new data from updatedUser;
});
}
});
的updatedUser看起來是這樣的:
{"__v":0,"_id":"535aa89d8b2766d012e14c21","activated":true,"role":"user","local":{"surname":"Carter","prename":"Rob","password":"459DS","email":"test"},"$promise":{},"$resolved":true}
服務:
app.service("APIService", function($resource) {
return $resource('http://localhost:3000/api/:route/:id', { route: "@route", id: "@id" }, {
'update': { method: 'PUT' }
});
});
我們可以看到'update'方法的服務代碼嗎?這似乎是你給這個方法一個回調,而不是AngularJS的'.then()'有承諾。 – kba
當然,我更新了我的第一篇文章。在我使用的另一個項目中:angular.copy(updatedUser,$ scope.users [index]);你認爲那是一種選擇嗎? – nofear87