如何在单击时更改单个元素的ng-repeat的ng-bind值

时间:2017-03-23 18:40:21

标签: javascript jquery angularjs angularjs-ng-repeat

我只是角度和开发购物车的初学者,现在有问题添加'添加!'当我点击“添加到购物车”按钮时的价值。 这是我的代码

<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!"; }

2 个答案:

答案 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