我想在嵌套的 angularjs ng-repeat中禁用带有复选框的按钮。我的意思是禁用当前 ng-repeat 数组中的按钮。我的代码如下例所示,
angular.module('test', [])
.controller('myController', ['$scope', function($scope) {
$scope.hasSelectedAnItem = false;
$scope.mainItems = [
{ dropdownlist: [
{OptionValue: 123, OptionName: "Adam", IsSelected: true},
{OptionValue: 234, OptionName: "Paul", IsSelected: false},
{OptionValue: 345, OptionName: "Jason", IsSelected: true},
{OptionValue: 464, OptionName: "Joe", IsSelected: false}
]},
{ dropdownlist: [
{OptionValue: 923, OptionName: "Adam2", IsSelected: true},
{OptionValue: 934, OptionName: "Paul2", IsSelected: false},
{OptionValue: 945, OptionName: "Jason2", IsSelected: true},
{OptionValue: 964, OptionName: "Joe2", IsSelected: false}
]}
];
$scope.canBeSaved = function(item) {
alert(item);
var found = false;
$scope.mainItems.forEach(function(item) {
if (item.IsSelected) {
found = true;
}
});
$scope.hasSelectedAnItem = found;
}
$scope.save = function() {
alert($scope.cat.IsSelected);
}
$scope.canBeSaved();
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.1/angular.min.js"></script>
<div ng-app="test" ng-controller="myController">
<div ng-repeat="items in mainItems">
<button ng-click="save()" ng-disabled="!hasSelectedAnItem" class="btn btn-sm btn-block btn-primary">
Save
</button>
<ul class="categories-list">
<li ng-repeat="cat in items.dropdownlist">
<label><input type="checkbox" id="cat-{{cat.OptionValue}}" ng-model="cat.IsSelected" ng-change="canBeSaved(cat.OptionValue)"/> {{cat.OptionName}}</label>
</li>
</ul>
</div>
</div>
需要你的帮助。带有代码的工作示例https://jsfiddle.net/malikzahid321/nc63hz90/24/谢谢