[英]Restangular/angularjs object not updating after using remove()
我在Angularjss中使用Restangular。 我有一個索引視圖,顯示所有消息。 我可以編輯這些消息並毫無問題地添加到列表中。 當我嘗試通過元素使用remove()
,顯示所有消息的索引頁不會更新。 我必須刷新頁面。 添加或編輯后,無需刷新頁面。
抱歉,未包含插件,但是我不確定如何獲取有效的示例,因為我的API是本地的。
我通過解決注入消息對象:
$routeProvider.when('/', {
templateUrl: 'messages-index.html',
controller: 'MessagesController',
resolve: {
messages: function(Restangular) {
return Restangular.all('messages').getList();
}
}
});
並使用ng-repeat
在索引視圖中顯示消息:
<li ng-repeat="message in messages | filter:query | filter:typeQuery class="messages__item">
<p>
<a href="#/messages/{{ message.id }}/">{{ message.message }}</a>
<a class="round secondary label">{{ message.type }}</a>
</p>
</li>
在編輯視圖中,我通過resolve注入特定消息:
$routeProvider.when('/messages/:messageId/edit', {
templateUrl: 'messages-edit.html',
controller: 'MessagesEditController',
resolve: {
message: function(Restangular, $route) {
return Restangular.one('messages', $route.current.params.messageId).get();
}
}
});
最后,我的MessagesEditController
如下所示:
.controller('MessagesEditController', ['message', '$scope', '$location', 'Restangular', function(message, $scope, $location, Restangular) {
var original = message;
$scope.message = Restangular.copy(original);
$scope.editMessage = function() {
$scope.message.put().then(function(){
$location.path('/');
}, function(response) {
$scope.formErrors = response.data.error.params;
});
};
$scope.deleteMessage = function() {
original.remove().then(function() {
$location.path("/");
});
};
}])
有人知道為什么刪除后在索引視圖中不更新我的消息對象嗎?
干杯!
由於某種原因,Restangular允許用戶在刪除操作后更新其$ scope。 根據使用情況,它可能會有用。
original.remove().then(function() {
$scope.messages = _.without($scope.messages, original);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.