Javascript 当对象为空时禁用按钮

Javascript 当对象为空时禁用按钮,javascript,angularjs,Javascript,Angularjs,当matchList=[]为空时,应禁用按钮ng disabled=checkIfDataExists。但当matchList=[]已满时,应启用该按钮。如何存档?谢谢 控制器: HTML: 修改控制器,如下所示 app.controller('gameplanController', ['currentAuth', '$scope', '$location', '$firebaseObject', '$firebaseArray', '$http', function (currentAuth

当matchList=[]为空时,应禁用按钮ng disabled=checkIfDataExists。但当matchList=[]已满时,应启用该按钮。如何存档?谢谢

控制器:

HTML:


修改控制器,如下所示

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
            });

        }]);

另一个选项是将匹配列表附加到$scope,并直接在NgDisable中检查长度

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

$scope.checkIfDataExists = function () {

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

    };

试试我的更改。

@jglom您是否将匹配列表附加到$scope?@jglom好的,太好了!删除var matchList=[];从你的函数中,你的按钮在哪里?你的按钮是否包装在ng if或ng repeat中?尝试创建一个具有上述问题的punker你在哪里调用了checkIfDataExists函数当matchlist已满或matchlist有数据时禁用它现在尝试我的更改?我认为这是因为你在函数中声明了matchlist=[]。试着把它去掉。
<button type="button" 
        class="btn btn-default button-main" 
        ng-disabled="!matchList.length > 0">
  {{ gametable }}
</button>
$scope.checkIfDataExists = function () {

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

    };
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
        }
    });

}]);