0

When matchList = [] is empty the button ng-disabled="checkIfDataExists" should be disabled. But when matchList = [] is full the button should be enabled. How to archive that? Thanks

Controller:

app.controller('gameplanController', ['currentAuth', '$scope', '$location', '$firebaseObject', '$firebaseArray', '$http', function (currentAuth, $scope, $location, $firebaseObject, $firebaseArray, $http) {

    var ref = firebase.database().ref("matches");
    var matchList = $firebaseObject(ref);

    matchList.$loaded().then(function (data) {

        $scope.checkIfDataExists = function () {
            var matchList = [];

            if(matchList.length === 0) {
                console.log("empty");
                $scope.checkIfDataExists = true
            } else {
                console.log("full");
                $scope.checkIfDataExists = false
            }

        };

    });

}]);

HTML:

<button type="button" class="btn btn-default button-main" ng-disabled="checkIfDataExists">{{ gametable }}</button>
5
  • Remove var matchList = []; from your function Commented Aug 24, 2017 at 9:22
  • Where is your button present? Commented Aug 24, 2017 at 9:24
  • Is your button wrapped inside a ng-if or ng-repeat? Commented Aug 24, 2017 at 9:29
  • Try to create a punker with the above problem Commented Aug 24, 2017 at 9:32
  • where did you called the checkIfDataExists function Commented Aug 24, 2017 at 9:59

4 Answers 4

1
app.controller('gameplanController', ['currentAuth', '$scope', '$location', '$firebaseObject', '$firebaseArray', '$http', function (currentAuth, $scope, $location, $firebaseObject, $firebaseArray, $http) {

    var ref = firebase.database().ref("matches");
    var matchList = $firebaseObject(ref);

    matchList.$loaded().then(function (data) {      

        var matchList = [];
        if(matchList.length === 0) {
            console.log("empty");
            $scope.checkIfDataExists = true
        } else {
            console.log("full");
            $scope.checkIfDataExists = false
        }
    });

}]);

Just try my changes.

Sign up to request clarification or add additional context in comments.

1 Comment

try my changes now
0

modify your controller simple as follow

app.controller('gameplanController', ['currentAuth', '$scope', '$location', '$firebaseObject', '$firebaseArray', '$http', function (currentAuth, $scope, $location, $firebaseObject, $firebaseArray, $http) {

            var ref = firebase.database().ref("matches");
            var matchList = $firebaseObject(ref);
            $scope.checkIfDataExists = false
            matchList.$loaded().then(function (data) {

                    $scope.checkIfDataExists = (matchList.length !== 0); //checkIfDataExists true when matchlist array has data
            });

        }]);

1 Comment

its disabled when matchlist is full (or matchlist has data)
0

Another option would be to attach the matchList to the $scope and check the length directly in the NgDisable like

<button type="button" 
        class="btn btn-default button-main" 
        ng-disabled="!matchList.length > 0">
  {{ gametable }}
</button>

2 Comments

@jglom did you attach the matchList to the $scope?
@jglom Ok, great!
0

Try this instead.

$scope.checkIfDataExists = function () {

        if(matchList.length === 0) {
            console.log("empty");
            return true;
        } else {
            console.log("full");
            return false;
        }

    };

1 Comment

well I think its because you declare the " matchList = []" inside the function. try to remove that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.