从列表中重新定义CRUD元素方法
Restangular CRUD element methods from list
我正在使用一个AngularJS应用程序的Restangular,但是我遇到了一些CRUD操作的问题。
我正在尝试修补并从列表中删除项目。无论我怎么做,这些错误总是出现:
这是我的代码块:TypeError:项目。$ Scope. deleteitem
TypeError:项目。$ Scope. requesttitems . restangular .all. customget .then.$ Scope. patchtitlechange
appModule
.controller('AppController', ['$scope', 'Restangular', '$rootScope', '$state', '$document',
function ($scope, Restangular, $rootScope, $state, $document) {
$scope.items = [];
$scope.requestItems = function (search_input, page, status) {
var request_params = {
"search": search_input,
"page": page,
"filter_status": status
};
Restangular.all('/myUrl/3/').customGET("items", request_params).then(
function (data) {
if (request_params.page == 1)
$scope.items = data.results;
else
$scope.items = $scope.items.concat(data.results);
$scope.metadata = {
"count": data.count,
"next": data.next,
"previous": data.previous
};
$scope.patchTitleChange = function (index) {
console.log($scope.items[index].name);
$scope.items[index].patch({name: $scope.items[index].name});
};
$scope.patchDescriptionChange = function (index) {
$scope.items[index].patch({description: $scope.items[index].description});
};
}
)
};
$scope.deleteItem = function (item) {
item.remove().then(function () {
var index = $scope.items.indexOf(item);
if (index > -1)
$scope.items.splice(index, 1);
})
};
我看了其他与我的问题相关的问题,并尝试了他们的答案和解决方案,但似乎没有一个奏效。
分配给items数组的元素不是正则化元素。你在分配数据。结果,它是纯JSON对象。为了有补丁,删除,等等…它们必须通过Restangular传递。重新规范或直接接收get/getList请求:
//Collection
$scope.items = Restangular.restangularizeCollection(null, data.results, 'myUrl/3/items');
//Individual Items
angular.forEach(data.results, function (item) {
$scope.items.push(Restangular.restangularizeElement(null, item, 'myUrl/3/items');
});
当重新正则化集合时,单个项也将被重新正则化,我只提到对结果集进行迭代,因为看起来您是在附加上面多个调用的结果。
参见:Restangular Methods
相关文章:
- 用嵌套函数和默认函数定义函数
- Dojo不解析自定义小部件的模板html中的小部件声明性
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 控制台返回var不是't定义,但它是
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- 如何检查管道中未定义的项目
- TypeError:无法读取属性'推'未定义的JavaScript
- jQuery自定义验证比较多个输入的序列
- 查看JS对象的所有键,甚至是getter定义的键
- fetch() 函数未在 Ubuntu Chromium 浏览器上定义
- $window.ga在AngularJS事件中未定义
- 如何在JQuery函数中定义一个值,然后调用另一个函数并使用该值
- 是否有任何方法可以使用jQuery替换在数组中定义值的文本
- 未捕获的TypeError无法读取未定义的属性socialsharing
- WebDriverException:tinyMCE未在selenium Web driver java中定义
- 从HTTPGET返回一个自定义对象列表,以便在Angular 2应用程序中使用
- 定义完全独立的样式信息
- 从列表中重新定义CRUD元素方法
- 如何在服务中定义 CRUD 操作 (AngularJS)