我只是角度和开发购物车的初学者,现在有问题添加'添加!'当我点击“添加到购物车”按钮时的价值。 这是我的代码
<div ng-repeat="item in products"> <a ng-click="additem(item,$index)">add to cart</a><span>{{cartadded}}</div>
JS
$scope.additem = function (product,index){$scope.productsList.push(product); var item=$scope.product[index]; item.cartadded="Added!"; }
答案 0 :(得分:0)
您需要逐个跟踪每个项目的状态。我还清理了一下你的代码:
<div ng-repeat="item in products">
<a ng-click="additem($index)" ng-hide="item.cartadded">add to cart</a>
<span ng-show="item.cartadded>Added!</span>
</div>
控制器
$scope.additem = function(index) {
var item = $scope.products[index];
$scope.productsList.push(item);
item.cartadded = true;
}
答案 1 :(得分:0)
所以最后这里是一个解决问题,以解决与此问题相关的其他人的帮助
<div ng-repeat="item in products">
<a ng-click="additem1($index)" >add to cart</a>
<span ng-show="item.cartadded">Added!</span>
</div>
JS
$scope.additem1 = function (index ){
var item = $scope.products[index];
$scope.productsList.push(item);
item.cartadded = true;
};
一些改进的答案,并感谢 AJ Funk