删除与Restangular进入 [英] Deleting entry with Restangular

查看:302
本文介绍了删除与Restangular进入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的AngularJS应用程序中使用Restangular。我有一个删除链接为每个项目的表。我想删除的项目,并有行自动删除。但随着事情只是将其从数据库中删除。我如何重构的事情,所以它的DOM会自动更新?

I am using Restangular in my AngularJS app. I have a table with a delete link for each item. I would like to delete the item and have the row automatically removed. But as things are it only deletes from DB. How can I refactor things so that it the DOM is updated automatically?

// The controller
angular.module('myApp').controller('ManageCtrl', function($scope, Restangular) {

  $scope.delete = function(e) {
     Restangular.one('product', e).remove();
  };

  Restangular.all('products').getList({}).then(function(data) {
    $scope.products = data.products;
    $scope.noOfPages = data.pages;
  });
});


 // The view
 <li ng-repeat="product in products">
   <a href="#" ng-click="delete(sheet._id)"></a>
  </li>

我也很想找到一个这样的例子 - 即使有角的资源。所有管理/数据表演示似乎从静态的数据。

I would also love to find an example of this - even with Angular resource. All the admin/data table demos seem to work from static data.

推荐答案

据Restangular 的https:/ /github.com/mgonto/restangular#restangular-methods 他们提到,你应该使用原来的商品和c你应该在你的HTML $ C $执行的操作,所以:

According to Restangular https://github.com/mgonto/restangular#restangular-methods they mention that you should use the original item and run an action with it, so in your html code you should:

 <li ng-repeat="product in products">
   <a href="#" ng-click="delete(product)"></a>
</li>

然后在你的控制器:

Then in your controller:

 $scope.delete = function( product) {
    product.remove().then(function() {
      // edited: a better solution, suggested by Restangular themselves
      // since previously _.without() could leave you with an empty non-restangular array
      // see https://github.com/mgonto/restangular#removing-an-element-from-a-collection-keeping-the-collection-restangularized

      var index = $scope.products.indexOf(product);
      if (index > -1) $scope.products.splice(index, 1);
   });
 };

注意他们使用 underscore.js没有将从数组中删除的元素。我想,如果他们张贴在他们的自述页面,例如这意味着一个.remove()函数不从集合中删除原始项目。这是有道理的,因为并不是每一个项目删除要从集合本身删除。

Notice they use the underscore.js without which will remove the element from the array. I guess that if they post that example in their readme page that means the .remove() function doesn't remove the original item from the collection. This makes sense, since not every item you remove you want removed from the collection itself.

此外,如果删除$ HTTP 请求失败会发生什么?你不想删除的项目,然后,你必须确保处理您code这个问题。

Also, what happens if the DELETE $HTTP request fails? You don't want to remove the item then, and you have to make sure to handle that problem in your code.

这篇关于删除与Restangular进入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆