当对象为空时禁用按钮

时间:2017-08-24 08:36:59

标签: javascript angularjs

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

控制器:

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>

4 个答案:

答案 0 :(得分:1)

SET @VMemberID := 0, @VCounter := 0;

/* Return the data */
SELECT
   [WordSpoken]
   ,[182] AS [Mem182_UniqueTally]
   ,[200] AS [Mem200_UniqueTally]
FROM (
   SELECT [WordSpoken], [MemberID], [UniqueTally]
   FROM (
          SELECT @VCounter := IF(@VMemberID != memberId, 0, @VCounter + 1) rowNumber, @VMemberID := memberId memberId
          FROM [dbo].[ut_test]
          ORDER BY [WordSpoken]
                 ,[MemberID]
                 ,[UniqueTally]
          ) AS [test]
   ) AS [tbl]
PIVOT (MAX(UniqueTally) FOR MemberID IN ([182],[200])) AS [pvt]
GO

试试我的修改。

答案 1 :(得分:0)

修改控制器简单如下

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

        }]);

答案 2 :(得分:0)

另一种选择是将matchList附加到$ scope并直接在NgDisable中检查长度,如

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

答案 3 :(得分:0)

试试这个。

$scope.checkIfDataExists = function () {

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

    };