简体   繁体   English

使用remove()后Restangular / angularjs对象未更新

[英]Restangular/angularjs object not updating after using remove()

I'm using Restangular in my Angularjss. 我在Angularjss中使用Restangular。 I have an index view that displays all messages. 我有一个索引视图,显示所有消息。 I can edit these messages and add to the list without problem. 我可以编辑这些消息并毫无问题地添加到列表中。 When I try to use remove() via the element, the index page displaying all the messages doesn't update. 当我尝试通过元素使用remove() ,显示所有消息的索引页不会更新。 I must refresh the page. 我必须刷新页面。 I do not need to refresh the page after an addition or edit. 添加或编辑后,无需刷新页面。

Sorry for not including a plunker but I'm not sure how to get a working example since my API is local. 抱歉,未包含插件,但是我不确定如何获取有效的示例,因为我的API是本地的。

I inject the messages object via resolve: 我通过解决注入消息对象:

$routeProvider.when('/', {
    templateUrl: 'messages-index.html',
    controller: 'MessagesController',
    resolve: {
        messages: function(Restangular) {
            return Restangular.all('messages').getList();
        }
    }
});

And use ng-repeat To display the messages in the index view: 并使用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, I inject the specific message via resolve: 在编辑视图中,我通过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, my MessagesEditController looks like this: 最后,我的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("/");
            });
        };
}])

Anyone know why my messages object is not updated in the index view after removal? 有人知道为什么删除后在索引视图中不更新我的消息对象吗?

Cheers! 干杯!

Restangular for some reason leaves it up to the user to update their $scope after a remove operation. 由于某种原因,Restangular允许用户在删除操作后更新其$ scope。 Depending on the use case it may be useful. 根据使用情况,它可能会有用。

original.remove().then(function() {
    $scope.messages = _.without($scope.messages, original);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM