1

我有一个包含一堆对象的数组。如果没有对象包含键“完成”的“真”值,我想禁用一个按钮。

//Here is the format for the array of objects:

$scope.todos = [{
                task:$scope.task,
                completed: false,
                localID:Date.now(),
                display: true
}];

//Here is the button I want to disable::

<button ng-click="clear()" class="btn" ng-disabled="">Clear Completed</button>

任何帮助表示赞赏。

4

4 回答 4

4

您可以将过滤器放在todos对象上并检查那里的长度。

标记

<button ng-click="clear()" class="btn" ng-disabled="(todos | filter: {completed:true}).length < 1">
     Clear Completed
</button>

工作小提琴

于 2015-08-21T06:42:38.110 回答
3

你可以检查类似

var app = angular.module('my-app', [], function() {})

app.controller('AppController', function($scope) {
  $scope.todos = [{
    task: $scope.task,
    completed: false,
    localID: Date.now(),
    display: true
  }];

  $scope.isDisabled = function() {
    return $scope.todos.some(function(item) {
      return item.display === true
    })
  }


})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="my-app" ng-controller="AppController">
  <input type="checkbox" ng-model="todos[0].display" <br />
  <button ng-click="clear()" class="btn" ng-disabled="isDisabled()">Clear Completed</button>
</div>

于 2015-08-21T06:42:48.703 回答
1
ng-disabled="(todos | filter: {completed:true}).length < 1"
于 2015-08-21T07:44:21.770 回答
0

你可以试试:

html:

<button ng-click="clear()" class="btn" ng-disabled="isDisabled()">Clear Completed</button>

在控制器中:

$scope.isDisabled = function () {
        var disabled = true;
        angular.forEach($scope.todos, function(todo){
            if(todo.completed === true){
                disabled = false;
            }
        });
        return disabled;
    };
于 2015-08-21T07:11:13.740 回答