当我向购物篮添加商品时,我想让用户在查看商品目录时看到他们已将该商品添加到购物篮中。目前,当我将商品添加到购物篮时,每个商品旁边都会显示消息added to basket
。我希望它出现在他们刚添加的产品旁边。
我该怎么做?
这是我到目前为止所获得的代码,它将项目添加到购物篮中:
$scope.addToBasket = function(item){
$scope.basket.push(item);
$scope.addedToBasket = 'added to basket';
}
<div ng-repeat="item in items">
<div ng-click="addToBasket(item)">
{{item.name}} {{item.price}} {{addedToBasket}}
</div>
</div>
答案 0 :(得分:1)
请尝试以下代码
$scope.addToBasket = function (item) {
$scope.basket.push(item);
angular.forEach($scope.items, function (objItem) {
objItem.addedToBasket = angular.equals(objItem, item) ? 'added to basket' : '';
});
}
<div ng-repeat="item in items">
<div ng-click="addToBasket(item)">
{{item.name}} {{item.price}} {{item.addedToBasket}}
</div>
</div>
希望这会对你有所帮助
答案 1 :(得分:0)
该属性必须按项而不是按类存在:
$scope.addToBasket = function(item){
$scope.basket.push(item);
item.customMessage="Added to basket";
}
<div ng-repeat="item in items">
<div ng-click="addToBasket(item)">
{{item.name}} {{item.price}} {{item.customMessage}}
</div>
</div>