如何更正阵列中的这种冗余
How to correct this redundancy in an array?
我正在用ionic构建一个应用程序,我需要将项目传递到另一个数组中,但是当我再次传递相同的项目时,它被认为是相同的项目 - 真的 - 但我需要它不会发生。我需要它们被认为是不同的,或者至少被认为是不同的。数组的每个项目(是一个对象)都有一个变量"qntt"(quantity)
当此错误发生时,而是将相同的项目放在数组中,再加上这个变量中的一个。 OBS:我在 html 中使用ng-repeat="item in items track by $index"
。
<ion-view title="Order">
<ion-content overflow-scroll="true" padding="true" style="background: url(img/background1.jpg) no-repeat center;" class="has-header" ng-controller="pedidoCtrl">
<button class="button button-dark button-small button-block" ng-click="showDelete = !showDelete">Remover</button>
<ion-list show-delete="showDelete">
<ion-item class="item-thumbnail-left item-remove-animate" ng-repeat="item in items track by $index">
<ion-delete-button class="ion-minus-circled" ng-click="deleteItem(item)"></ion-delete-button>
<img src="{{item.img}}">
<h2>{{item.nome}}</h2>
<div class="row">
<div class="col col-60">
<h2>R{{item.subtotal | currency}}</h2>
</div>
<div class="col col-15">
<button class="button button-clear button-assertive button-small icon ion-minus" ng-click="subQtt(item)"></button>
</div>
<div class="col col-10">
<h2>{{item.qtt}}</h2>
</div>
<div class="col col-15">
<button class="button button-icon-right button-clear button-assertive button-small icon ion-plus" ng-click="addQtt(item)"></button>
</div>
</div>
</ion-item>
</ion-list>
<button style="text-align:right;" class="button button-assertive button-block icon-left ion-cash">R{{total | currency}}</button>
<div style="margin-right:-20px;">
<button style="left:-10px;" class="button button-dark button-large button-full icon ion-android-cart">Order</button>
</div>
</ion-content>
.controller('orderCtrl', function($scope, productService) {
$scope.items = null;
$scope.items = productService.getProducts();
$scope.deleteItem = function(item){
$scope.items.splice($scope.items.indexOf(item), 1);
};
$scope.$watchCollection('items', function(array) {
if (array) {
$scope.total = array.reduce(function(total,item) {
return total + item.subtotal;
},0);
}
$scope.addQtt = function(item){
item.qtt = item.qtt + 1;
item.subtotal = item.price * item.qtt;
$scope.total = array.reduce(function(total,item) {
return total + item.subtotal;
},0);
};
$scope.subQtt = function(item){
if(item.qtt > 1)
{
item.qtt--;
item.subtotal = item.price * item.qtt;
}
else
{
$scope.items.splice($scope.items.indexOf(item), 1);
}
$scope.total = array.reduce(function(total,item) {
return total + item.subtotal;
},0);
};
});
})
.service('productService', [function(){
var productList = [];
var addProduct = function(product) {
productList.push(product);
};
var getProducts = function(){
return productsList;
};
return {
addProduct: addProduct,
getProducts: getProducts,
};
}]);
这是因为您没有在控制器中addProduto()
调用服务方法。尝试在 $scope.addQnt()
方法中调用produtoService.addProduto()
,如下所示,它应该可以工作:
$scope.addQnt = function(item){
item.qntd = item.qntd + 1;
item.subtotal = item.preco * item.qntd;
$scope.total = array.reduce(function(total,item) {
return total + item.subtotal;
},0);
// Add this line to retain your addition in service
produtoService.addProduto(item);
// Get updated list of products
$scope.items = productoService.getProdutos();
// Alternatively use '$scope.items.push(item)'
};
此外,您需要通过触发对productoService.getProdutos()
的调用来刷新$scope.items
,尽管您可以通过推入$scope.items.push(item)
相关文章:
- 缓存的最佳实践.避免冗余缓存
- UMD:正在分配给模块,导出冗余
- 如何更正阵列中的这种冗余
- Regex删除涉及零或一的冗余乘法/除法
- 删除方法中的冗余代码
- 多选择器冗余
- 简化冗余jquery's代码
- 排列具有相同数据的多个对象(减少冗余)
- Meteor模板-继承或外包事件以避免代码冗余
- HTML-减少HTML代码冗余
- 如果我两次使用相同的反应/冗余组件,它们会共享状态吗?
- 等待多个 ipc 调用完成,然后再继续电子/冗余
- 在 Javascript 中模拟“IN”运算符以简化冗余逻辑 OR 的最佳解决方案是什么?
- elasticsearch:保留冗余(非规范化)数据或保留 id 列表以进行交叉引用
- 如何获取调度冗余
- 编写函数以防止冗余
- 如何避免客户端验证中的冗余条件
- 同构反应-路由器-冗余同步历史中间件
- ExtJS - 如何创建可重用的函数以避免代码冗余
- 构建本地化反应/冗余应用程序的存储