0

I am trying to disable a button based on an angular object array being empty. My HTML below..

<ons-button ng-click="do stuff" ng-disabled="testFunc()">Add</ons-button>

This is inside an ng-app HTML and inside of a controller. My test function is below...

$scope.objs = [];
$scope.testFunc = function() {
    if($scope.objs == null) {
        return true;
    }
    else {
        return false;
    }
};

I have my function and the $scope.objs = []; declared inside the controller, and the button is not being disabled. Am I doing something wrong?

EDIT: Appears the function is not even firing, will ng-disabled not fire once DOM is loaded. Also, could there be an issue with declaring the array and maybe it wasnt declared intime for the function to test it? Should I add an OR testing if its undefined?

Eddie K
  • 13
  • 2
  • 6

1 Answers1

0

$scope.objs is an array, which is different than null. Try to check its length instead...

if($scope.objs.length === 0) {
    return true;
}
else {
    return false;
}
Anthony Chu
  • 37,170
  • 10
  • 81
  • 71
  • okay, i can try that, i put a console.log in both if and else and nothing is logging, almost like the testFunc isnt even firing? will it not auto fire once the DOM is loaded? – Eddie K Jun 02 '14 at 20:47
  • Just noticed the ng-disabled is on a custom directive. I don't think it'll work. – Anthony Chu Jun 02 '14 at 20:53