javascript - AngularJS - 限制增量和减量

标签 javascript angularjs angularjs-scope

我有两个按钮,一个用于递增,另一个用于递减我的范围。

我想要:分钟。数量 = 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/

相关文章:

javascript - 将 iFrame 添加到 D3 可折叠树插件上的每个节点

angularjs - Protractor:在 Iframe 中测试 Angular 应用程序

javascript - 从 .then() 内更新 ng-show

javascript - 类型错误 : Cannot read property 'go' of undefined

javascript - PKCS#12 (.pfx .p12) 使用 node.js 生成?

javascript - JSON 循环不工作

php - 提交一个设置了action属性的表单,同时有一个javascript代码来处理提交

AngularJS Protractor 测试 - E2E 测试应该有固定装置吗?

javascript - 使用 ng-options 进行错误迭代

angularjs - 如何使用带有 AngularJS 的表元素显示子级数组