我有两个按钮,一个用于递增,另一个用于递减我的范围。
我想要:分钟。数量 = 0 和最大。数量 = 50。
我尝试过这个:
myApp.controller('StepCtrl', function($scope, $timeout, $state, $stateParams) {
$scope.event = {
priceid: 0,
}
if ($scope.event.priceid === 0) {
console.log("This is 0")
$scope.nextPrice = function() {
$scope.event.priceid += 5;
}
$scope.previousPrice = function() {
$scope.event.priceid = 0;
}
} else if ($scope.event.priceid === 50) {
console.log("This is 50")
$scope.nextPrice = function() {
$scope.event.priceid = 50;
}
$scope.previousPrice = function() {
$scope.event.priceid -= 5;
}
} else {
console.log("This is into 0 and 50")
$scope.nextPrice = function() {
$scope.event.priceid += 5;
}
$scope.previousPrice = function() {
$scope.event.priceid -= 5;
}
}
})
但是,我可以有像 -40 或 95 这样的值,我认为我的“If”和“Else”被忽略。
你有什么想法吗?
谢谢回复。 :)
最佳答案
我必须查看您的完整 Controller 才能了解完整情况。但我认为问题在于你的表达式是在启动时评估一次而不是实时评估。
我的解决方案是用此替换 if-else 语句。
$scope.event = {
priceid: 0,
}
$scope.nextPrice = function() {
if($scope.event.priceid < 50)
$scope.event.priceid += 5;
}
$scope.previousPrice = function() {
if($scope.event.priceid > 0)
$scope.event.priceid -= 5;
}
这样,每次调用 $scope.previousPrice() 或 $scope.nextPrice() 时都会评估您的条件,而不是在运行时分配这些方法。
关于javascript - AngularJS - 限制增量和减量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38100086/