$scope 函数和 ng-disable 不起作用
$scope function and ng-disable not functioning
下面是我的 angular 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 ,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 时提醒用户?我尝试将 if 语句添加到 buy() 函数中,但它不起作用。我的目标是在总预算低于 0 时提醒用户。
备选
我厌倦的另一个选择是如果函数 getTotal
的结果小于 0,则禁用 "save team" 按钮。下面是我尝试的 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>
在这里,您在 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')
}
},
你能不能试试重写函数如下。您需要更改 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);
};
$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
}
},
下面是我的 angular 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 ,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 时提醒用户?我尝试将 if 语句添加到 buy() 函数中,但它不起作用。我的目标是在总预算低于 0 时提醒用户。
备选
我厌倦的另一个选择是如果函数 getTotal
的结果小于 0,则禁用 "save team" 按钮。下面是我尝试的 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>
在这里,您在 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')
}
},
你能不能试试重写函数如下。您需要更改 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);
};
$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
}
},