最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular - Enable button only if object inside an array contains completed:true - Stack Overflow

programmeradmin0浏览0评论

I have an array that contains a bunch of objects. If there are no objects that contains a "true" value for the key "pleted", I would like to disable a button.

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

$scope.todos = [{
                task:$scope.task,
                pleted: 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>

Any help is appreciated.

I have an array that contains a bunch of objects. If there are no objects that contains a "true" value for the key "pleted", I would like to disable a button.

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

$scope.todos = [{
                task:$scope.task,
                pleted: 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>

Any help is appreciated.

Share Improve this question edited Jun 22, 2018 at 16:39 georgeawg 49k13 gold badges77 silver badges98 bronze badges asked Aug 21, 2015 at 6:39 user2916134user2916134
Add a ment  | 

4 Answers 4

Reset to default 4

You could place filter over your todos object and check for there length.

Markup

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

Working Fiddle

You can check something like

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

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

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


})
<script src="https://ajax.googleapis./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>

ng-disabled="(todos | filter: {pleted:true}).length < 1"

you can try:

html:

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

in controller:

$scope.isDisabled = function () {
        var disabled = true;
        angular.forEach($scope.todos, function(todo){
            if(todo.pleted === true){
                disabled = false;
            }
        });
        return disabled;
    };
发布评论

评论列表(0)

  1. 暂无评论