简体   繁体   中英

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. I assume a filter but it might be ng-change that I should be using.

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.

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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