I am currently using Restangular within my AngularJS application. I have an index view that displays all messages, and I can edit and add messages to the list without any issues. However, when I attempt to use the remove()
function on an element, the index page displaying all the messages does not update unless I manually refresh the page. It's worth mentioning that I do not encounter this issue when adding or editing messages.
Unfortunately, I don't have a working example in Plunker as my API is local and cannot be easily replicated for demonstration purposes.
The messages object is injected via resolve:
$routeProvider.when('/', {
templateUrl: 'messages-index.html',
controller: 'MessagesController',
resolve: {
messages: function(Restangular) {
return Restangular.all('messages').getList();
}
}
});
To display the messages in the index view, I utilize 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>
In the edit view, the specific message is injected via 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();
}
}
});
Lastly, the MessagesEditController
contains the following code:
.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("/");
});
};
}])
If anyone has insights into why the messages object is not updated in the index view after removal, your input would be greatly appreciated!
Cheers!