<div class="col-md-2">
<md-button class="md-raised md-primary" ng-click="deleteDiv()">Remove</md-button>
</div>
scope.deleteDiv = function() {
alert(scope.itemsToAdd.length);
if(scope.itemsToAdd.length > 1) {
scope.itemsToAdd.splice(scope.itemsToAdd.length,1)
}
};
嗨。我是AngularJs的新手。我对删除动态创建的div有疑问。 itemsToAdd是一个包含四个字段的数组。我使用push和ng-repeat动态添加这些字段。单击“删除”按钮时,我得到数组的长度,只有在大于1时才删除。我发布的删除过程,是是正确的?我在哪里做错了?谢谢
答案 0 :(得分:2)
您需要将要删除的元素的索引作为splice函数的参数传递:
Resolved
答案 1 :(得分:0)
通过这个,它是你想要的类型之一
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="myApp" ng-controller="todoCtrl">
<h2>My Todo List</h2>
<form ng-submit="todoAdd()">
<input type="text" ng-model="todoInput" size="50" placeholder="Add New">
<input type="submit" value="Add New">
</form>
<br>
<div ng-repeat="x in todoList">
<input type="checkbox" ng-model="x.done"> <span ng-bind="x.todoText"></span>
</div>
<p><button ng-click="remove()">Remove marked</button></p>
<script>
var app = angular.module('myApp', []);
app.controller('todoCtrl', function($scope) {
$scope.todoList = [{todoText:'Clean House', done:false}];
$scope.todoAdd = function() {
$scope.todoList.push({todoText:$scope.todoInput, done:false});
$scope.todoInput = "";
};
$scope.remove = function() {
var oldList = $scope.todoList;
$scope.todoList = [];
angular.forEach(oldList, function(x) {
if (!x.done) $scope.todoList.push(x);
});
};
});
</script>
答案 2 :(得分:0)
<tr ng-repeat="person in persons track by $index">
<td>{{person.name}} - # {{person.id}}</td>
<td>{{person.description}}</td>
<td nowrap=nowrap>
<a href="#!/edit"><i class="icon-edit"></i></a>
<button ng-click="delete($index)"><i class="icon-minus-sign"></i></button>
</td>
</tr>
并在控制者中
$scope.delete=function(index){
$scope.persons.splice(index,1);
}