javascriptangularjsangularjs-filterangularjs-ng-disabled

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


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

//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>

Any help is appreciated.


Solution

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

    Markup

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

    Working Fiddle