gpt4 book ai didi

javascript - Angular JS : Change only clicked button text in ng-repeat

转载 作者:行者123 更新时间:2023-11-29 19:11:38 24 4
gpt4 key购买 nike

我是 Angular JS 的新手。我有一些产品要显示在我使用 ng-repeat 显示的主页中。每个产品都有一个“添加到购物车”按钮,该按钮也使用 ng-repeat 显示。我的问题是,我想更改我正在单击的特定按钮的文本。我使用了“切换”,所有按钮的文字都在改变。请有人帮我解决这个问题。

我的 HTML 代码:

<div class="col-md-3 col-sm-6" ng-repeat="item in products">
<div class="thumbnail">
<image src="{{item.imageUrl}}"></image>
</div>
<div>
<button class="btn btn-primary" ng-click="toggle(item)">{{buttonText}}</button>
</div>
</div>

我的 Angular js代码:

var toggle = false;  
$scope.toggle = function(item){
$scope.buttonText = toggle ? 'Add To Cart' : 'Remove From Cart';
toggle=!toggle;
}

最佳答案

您需要为传入的项目添加一个属性。然后使用该属性。更改范围会更改所有实例。

var toggle = false;  
$scope.toggle = function(item){
item.buttonText = toggle ? 'Add To Cart' : 'Remove From Cart';
toggle=!toggle;
}

<div class="col-md-3 col-sm-6" ng-repeat="item in products">
<div class="thumbnail">
<image src="{{item.imageUrl}}"></image>
</div>
<div>
<button class="btn btn-primary" ng-click="toggle(item)">{{item.buttonText}}</button>
</div>
</div>

有点像上面

关于javascript - Angular JS : Change only clicked button text in ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38436420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com