简体   繁体   English

AngularJS筛选两个小于或等于value的选择输入

[英]AngularJS Filter two select inputs with values less than or equal to value

I would like to setup two select inputs so that the combined value of either select input should be less than or equal to a specific value. 我想设置两个选择输入,以便每个选择输入的组合值应小于或等于特定值。 Newbie to the AngularJS world so I may have overlooked something. AngularJS世界的新手,所以我可能忽略了一些东西。 I assume a filter but it might be ng-change that I should be using. 我假设使用过滤器,但可能应该使用ng-change。

You can create an object which represents your two inputs. 您可以创建一个代表您的两个输入的对象。

$scope.inputs = {
    'A': 0,
    'B': 0
  };

Use an ng-change to call a function which checks the sum of all inputs and makes sure they are less than the max value. 使用ng-change调用一个函数,该函数检查所有输入的总和,并确保它们小于最大值。

<select ng-model="inputs.A" ng-options="number for number in numbers" ng-change="verifySum('A');"></select>

The function will take the key of the value being changed, and will update this value if the sum goes over the max. 该函数将使用要更改的值的键,如果总和超过最大值,则将更新此值。

  $scope.verifySum = function(key) {
    var sum = $scope.inputs.A + $scope.inputs.B
    if(sum >= $scope.maxVal) {
      $scope.inputs[key] -= (sum - $scope.maxVal);
    }
  }

http://plnkr.co/edit/vK0cfh7G3O3yKYCAzAOI?p=preview http://plnkr.co/edit/vK0cfh7G3O3yKYCAzAOI?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM