我正在使用带有AngularJS应用程序的Restangular并从一个简单的CRUD应用程序开始.我遇到的问题是了解如何将新项目添加到预先存在的集合中.
在控制器内,预先存在的集合在服务中获得并在范围内可用:
$scope.data.items = Restangular.all('items');
通过表单创建一个新项目并在服务器上创建:
$scope.data.items.post(newitem);
我想将这个新项目添加到预先存在的集合中但不确定如何. restangular sample再次调用Restangular.all(‘items’),鉴于所有信息都存在,这似乎非常浪费.理想情况下,我想做这样的事情:
$scope.data.items.post(newitem).then(function(response) { $scope.data.items.push(newitem); });
解决方法
它不起作用,因为您分配给data.items的是一个承诺.
如果您将其更改为以下它应该工作:
Restangular.all('items').then(function(items) { $scope.data.items = items });
希望它有效!