如何删除列表项Angular
How to delete list item Angular?
当我点击控制台中的删除按钮时
angular.js:12416类型错误:无法读取未定义的属性"indexOf"
nbsp nbsp 在m.$scope.removeCompany(app.js:21)
nbsp nbsp at fn(eval at(angular.min.js:1),:4:334)
nbsp nbsp 在f(angular.js:23371)
nbsp nbsp 下午$eval(angular.js:15878)
nbsp nbsp m.$apply(angular.js:15978)
nbsp nbsp 位于HTMLAnchorElement。(angular.js:23376)
nbsp nbsp 在HTMLAnchorElement.Hf.c(angular.js:3293)
if (!localStorage.getItem("companys")) {
localStorage.setItem("companys", JSON.stringify([]));
};
(function() {
var app = angular.module('myApp', []);
app.controller('ListController', function($scope){
this.retrieveCompanys = function() {
return JSON.parse(localStorage.getItem('companys'));
}
this.addToStorage = function(company){
this.companys.push(company);
localStorage.setItem('companys', JSON.stringify(this.companys));
}
this.companys= this.retrieveCompanys();
$scope.removeCompany = function (item) {
debugger;
var index= $scope.companys.indexOf(item);
$scope.companys.splice(index,1);
};
$scope.add = false;
$scope.togglechild = function() {
$scope.add = !$scope.add;
};
});
})();
<html ng-app="myApp">
<body class="container" ng-controller="ListController as list">
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4" ng-controller = "AddController as addCtrl">
...........
</div>
<div class="col-xs-12 col-sm-12 col-md-8 col-lg-8" >
<h3 class="text-center">List of Company</h3>
<table class="table">
<tr>
<th class="col-xs-1 col-sm-1 col-md-1 col-lg-1"></th>
<th class="col-xs-5 col-sm-5 col-md-5 col-lg-5">Name Company</th>
<th class="col-xs-2 col-sm-2 col-md-2 col-lg-2 text-center">Own earnings</th>
<th class="col-xs-2 col-sm-2 col-md-2 col-lg-2 text-center">Total earnings</th>
<th class="col-xs-2 col-sm-2 col-md-2 col-lg-2 text-center">Edit/Delete</th>
</tr>
<tr ng-repeat="company in list.companys track by $index">
<td class="col-xs-1 col-sm-1 col-md-1 col-lg-1 text-center">
<a href="#{{'demo'+$index}}" data-toggle="collapse"><span class="glyphicon glyphicon-eye-open"></span></a>
</td>
<td class="col-xs-6 col-sm-6 col-md-6 col-lg-6 text-center">
<b ng-hide="editing" ng-click="editing = true">{{company.name_company}}</b>
<form ng-show="editing" ng-submit="editing = false">
<button class="btn" type="submit"><span class="glyphicon glyphicon-ok"></span></button>
<input type="text" ng-model="company.name_company" placeholder="Name" ng-required>
</form>
</td>
<td class="col-xs-2 col-sm-2 col-md-2 col-lg-2 text-center">
<span ng-hide="editing" ng-click="editing = true">{{company.annual_earnings + " $"}}</span>
<form ng-show="editing" ng-submit="editing = false">
<input type="text" ng-model="company.annual_earnings" placeholder="Annual earnings" ng-required>
</form>
</td>
<td class="col-xs-2 col-sm-2 col-md-2 col-lg-2 text-center">
</td>
<td class="col-xs-2 col-sm-2 col-md-2 col-lg-2 text-center">
<a ng-click="editing = true" title="Edit Data"><span class="glyphicon glyphicon-edit"></span></a> 
<a ng-click="removeCompany($index)" title="Delete"><span class="glyphicon glyphicon-remove-sign"></span></a> 
<a ng-click="togglechild()" title="Add Child Company"><span class="glyphicon glyphicon-plus-sign"></span></a>
</td>
</tr>
</table>
</div>
</body>
</html>
问题出在您的代码上。您将companys
的值存储在this.companys
而不是$scope.companys
中,并尝试使用$scope.companys
而不是this.companys
访问它。您不应该使用this.
,因为那样您就必须处理作用域,而应该使用$scope
。
if (!localStorage.getItem("companys")) {
localStorage.setItem("companys", JSON.stringify([]));
};
(function() {
var app = angular.module('myApp', []);
app.controller('ListController', function($scope){
function retrieveCompanys() {
return JSON.parse(localStorage.getItem('companys'));
}
this.addToStorage = function(company){
$scope.companys.push(company);
localStorage.setItem('companys', JSON.stringify($scope.companys));
}
$scope.companys= retrieveCompanys();
$scope.removeCompany = function (index) {
$scope.companys.splice(index,1);
};
$scope.add = false;
$scope.togglechild = function() {
$scope.add = !$scope.add;
};
});
})();
David是正确的。但还有另一个问题。您正在使用$index
作为参数调用函数$scope.removeCompany
。
CCD_ 10。
在你的情况下,这意味着你在公司中搜索一个等于$index
的值。您将找不到任何,这将导致var index
等于-1。如果使用splice
并将-1作为第一个参数,则将始终在数组中的最后一个项上开始拼接。
$scope.removeCompany = function (item) {
var index = $scope.companys.indexOf(item);
$scope.companys.splice(index,1);
};
所以你应该这样称呼removeCompany:
<a ng-click="removeCompany(company)" title="Delete">
或者将控制器中的代码更改为:
$scope.removeCompany = function (index) {
$scope.companys.splice(index,1);
};
我建议更改函数的调用方式。
- 从HTTPGET返回一个自定义对象列表,以便在Angular 2应用程序中使用
- Angular:使用选择列表选择过滤代码中的对象
- 基于使用angular在下拉列表中选择的对象,迭代对象内部的对象
- 如何从值的Angular下拉列表中获取ID
- 使用angular显示复杂json中的列表
- 什么'是从对象列表中一次编辑一个对象的正确Angular/Firebase方法
- Angular JS在无序列表中的2个数组中重复
- 如何在 *ngFor 列表中显示单击的元素,使用 Angular 2 隐藏其他元素
- 在javascript/angular中创建播放列表(按值复制数组,但按引用设置嵌套对象)
- Angular 是否使用 DOM 差异,或者它必须“重新渲染”每个列表项
- 如何将Angular Js与谷歌地图结合使用单击标记时渲染人员列表
- 带有dataItem数组和angular ng点击的Kendo树列表模板
- Angular 2:用HTTP响应填充复选框列表
- 带有Angular JS解析列表的picklest
- 无法使用Angular.js从列表中正确筛选数据
- 如何将列表从C#angular传递到angular控制器
- 让Angular.js显示json数据的html列表
- Angular Material,侧边栏中的列表不可滚动
- 根据Angular JS中的属性值在列表中查找对象
- 如何从angular.js生成的下拉列表中将值附加到DOM