$ scope函数和ng-disable不起作用

时间:2017-11-08 14:08:02

标签: javascript angularjs web-applications angularjs-scope

以下是我的角度JS Web App的JS:

$scope.total = 50000000;

var ref = databaseService.players;


$scope.players = $firebaseArray(ref);


$scope.picked = [];
$scope.history = [];

 $scope.buy = function(player) {
        //remove if already added locally
        var index = $scope.history.indexOf(player);
        var index2 = $scope.picked.indexOf(player.id);
        if(index>=0 || index2>=0 ){
            $scope.history.splice(index,1);
            $scope.picked.splice(index2,1);
            PlayerService.removePlayerFromSelection(player);
            return;
        }

        //max 6 allowed
        if($scope.history.length>=6 || $scope.picked.length>=6){
            alert('max 6 allowed');
            return;
        }
        //to make sure moeny doesn't go below $50,000,000
        if($scope.total<0){
            alert('Oops! You have run out of cash');
            return;
        }

        var selected = $scope.history.reduce(function(a,b){
            a[b.position] = (a[b.position] || 0) + 1;
            return a;
        }, {}) || {};

        if(!selected[player.position] || selected[player.position]<2 && $scope.total>0){
            $scope.history.push(player);
            $scope.picked.push(player.id);
            PlayerService.addPlayerToSelection(player);       
        }else{
            alert('You can add only two players per position');

        }
      };

      $scope.getTotal = function(){
        return $scope.history.reduce(function(tot, p){
            tot = tot - p.price;
            return tot;
            if (tot <0){
                alert('Oops! You have run out of money')
            }
        }, $scope.total);
      }; 
  $scope.saveTeam = function(){
   userRef.set( $scope.picked);       
   $location.path('/mySquad');
   };

我的问题

当总数低于0时,有没有办法提醒用户?我尝试在buy()函数中添加一个if语句,但它没有用。我的目标是在总预算低于0时向用户发出警报。

替代

我厌倦的另一个选择是让#34;保存团队&#34;如果函数getTotal的结果导致值小于0,则禁用该按钮。以下是我尝试但未成功的HTML代码:

 <button type="submit" class="btn btn-lg btn-primary btn-md " ng-disabled="history.length<6" ng-disabled="getTotal<0" ng-click="saveTeam()" > Save Team </button>

3 个答案:

答案 0 :(得分:0)

这里,你的代码在return语句后不会执行。如果有条件,请移除return tot或将其移至下方。

 $scope.getTotal = function(){
    return $scope.history.reduce(function(tot, p){
        tot = tot - p.price;
        return tot; // wrong place of return statement
        if (tot <0){ // it won't execute, coz, it was returned before line itself
            alert('Oops! You have run out of money')
        }
    },

答案 1 :(得分:0)

您可以尝试重写该功能,如下所示。您需要更改return语句的位置

$scope.getTotal = function(){
        return $scope.history.reduce(function(tot, p){
            tot = tot - p.price;

            if (tot <0){
                alert('Oops! You have run out of money')
            }
           return tot; //changed location of this line
        }, $scope.total);
      }; 

答案 2 :(得分:0)

$scope.getTotal = function(){
 return $scope.history.reduce(function(tot, p){
    tot = tot - p.price;

    if (tot <0){ 
        alert('Oops! You have run out of money')
        return tot; // If you place here once you come here it will return
    }
},