数组拼接删除了 AngularJS 中的错误元素
Array splice removes the wrong element in AngularJS
我阅读了其他人的所有其他问题和答案,但仍然无法弄清楚。
.HTML:
<table class="table table-striped" ng-show="coffees.length>0">
<thead>
<tr>
<th>Drink</th>
<th>Price</th>
<th>Number per week</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="c in coffees">
<td>{{c.type}}</td>
<td>{{c.price | currency:"£"}}</td>
<td>{{c.numberpw}}</td>
<td><a href ng-click="removeCoffee($index)">X</a></td>
</tr>
</tbody>
</table>
应用.js:
var app = angular.module("calculator",[]);
app.controller('pageController',function($scope){
$scope.coffees=[];
});
app.controller('coffeeController',function($scope){
$scope.addCoffee=function(coffee){
$scope.coffees.push(coffee);
$scope.coffee={};
}
$scope.removeCoffee=function(el){
$scope.coffees.splice($scope.coffees[el],1);
}
});
coffeeController 嵌套在 pageController 中,所以我可以在 coffeeController 中访问 $scope.coffees。addCoffee 函数接受如下所示的对象:
<select name="CoffeeType" ng-model="coffee.type" ng-options="type for type in
['Espresso','Latte']" class="form-control" required>
<option value="">Please select</option>
</select>
<input type="text" placeholder="£00.00" ng-pattern="/^0|[1-9][0-9]*$/" ng-model="coffee.price" name="CoffeePrice" class="form-control" required />
<select name="NumberPerWeek" class="form-control" ng-model="coffee.numberpw" ng-options="n for n in [1,2,3,4,5]" required>
<option value="">Please select</option>
</select>
<input type="submit" class="btn btn-primary pull-left" value="Add Drink" ng-click="addCoffee(coffee)" />
它完美地添加了对象,但每次都删除了错误的对象。
splice
需要开始/计数整数。 $scope.coffees[el]
是一个对象,但您正在将$index
传递给该方法。更新删除方法,如下所示:
$scope.removeCoffee=function(el){
$scope.coffees.splice(el,1);
}
相关文章:
- 未捕获错误:元素缓存中id为x的DOM元素与DOM中的元素不同
- 从父元素AngularJS中获取多个输入字段的值
- 将持久活动类附加到导航栏范围中的导航元素(AngularJS)
- 展开/折叠树中的子元素(AngularJS)
- 来自Svc的数据未建模到视图元素AngularJS中
- 角度多选下拉框给出错误元素.多选不是一个函数
- 聚焦和模糊错误元素触发的事件
- 错误:“元素当前不可见,因此可能无法与硒交互”
- 使用 Directives 注入 DOM 元素 AngularJS
- 数组拼接删除了 AngularJS 中的错误元素
- 木偶嵌套布局视图未呈现,显示错误“元素必须存在于 DOM 中”
- materializecss 日期选择器量角器(错误:元素在点处不可单击)
- 获取元素angularJS的点击事件的html elemnt类型
- 从循环中隐藏元素(AngularJS)
- 真正阻止元素绑定-取消绑定元素-AngularJS
- jQuery在ngView加载的元素[AngularJS]上找不到元素
- 失败:未知错误:元素在点(x,x)-角度/量角器处不可单击
- JSON级别()错误:元素列表后缺少]
- CSS第n个子代不'无法处理动态隐藏元素[AngularJS]
- 如何不允许生成错误元素