gpt4 book ai didi

javascript - 禁用和启用 ng-repeat 中的按钮

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

    <div ng-repeat="item in Items">                          
<button ng-init="checkIfDisabled[item.name] = true"
ng-disabled ="checkIfDisabled[item.name] ===true "></button>
</div>

我有一个名为 cart 的变量,其中有对象。我希望仅当 cart 变量在其任何对象中具有该按钮时才启用该按钮。

将它们添加到购物车时,我这样做了:

 var itemname = item.name;
$rootScope.checkIfDisabled[itemname] = false;

但这似乎不起作用。有解决办法吗?

按钮最初被禁用,但当值更改为 false 时不会启用。

最佳答案

You need to have an array which will hold all the cart items and later you can use conditions to enable/disable them..

angular.module('myApp', [])
.controller('myController', function($scope) {
$scope.Items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
$scope.cartItems = [];
$scope.addToCart = function(item) {
$scope.cartItems.push(item);
};
$scope.doesExist = function(item) {
return $scope.cartItems.indexOf(item) !== -1;
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html lang="en" ng-app="myApp">

<head>
<meta charset="UTF-8">
<title></title>
<script src="js/angular.min.js"></script>
</head>

<body ng-controller="myController">
<div ng-repeat="item in Items">
<button ng-disabled="doesExist(item)" ng-bind="item" ng-click="addToCart(item)"></button>
<br/>
<br/>
</div>
</body>

</html>

关于javascript - 禁用和启用 ng-repeat 中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32650163/

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