繁体   English   中英

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

[英]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.

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