根据angularjs中的下拉值设置输入类型的最小值,最大值=数量

时间:2017-08-17 16:18:16

标签: angularjs

如何仅根据freqValue从html设置min,max值。

  1. 如果freqValue = 1则min = 1,max = 20
  2. ELSE如果freqValue = 2则min = 1,max = 45
  3. ELSE freqValue = 3 then min = 1,max = 100
  4. <input type="number" min="0" max="100" ng-model="interval" />
    <select ng-model="freqValue" ng-options="f.key as f.value for f in freq">
    </select>
    

2 个答案:

答案 0 :(得分:1)

您可以将包含这些频率值的地图作为其键存储每个频率的minmax值,以便在模型更改时,angularjs将重新绑定min和基于地图索引的max值,即freqValue变量。

例如:

控制器:

$scope.map = {
    '1': { min: 1, max: 20 },
    '2': { min: 1, max: 45 },
    '3': { min: 1, max: 100 },
};

模板:

<input type="number"
    min="{{ map[freqValue].min }}"
    max="{{ map[freqValue].max }}"
    ng-model="interval" />
<select
    ng-model="freqValue"
    ng-options="f.key as f.value for f in freq">
</select>

答案 1 :(得分:0)

试试这个

(function() {
  var app = angular.module('app', []);
  app.controller('main', ['$scope', function($scope) {
    var vm = this;

    vm.freq = [{key:1},{key:2},{key:3}]

    vm.setMinMax = function() {
      if (vm.freqValue == 1) {
        vm.min = 1;
        vm.max = 20;
      }
      if (vm.freqValue == 2) {
        vm.min = 1;
        vm.max = 45;
      }
      if (vm.freqValue == 3) {
        vm.min = 1;
        vm.max = 100;
      }
    }

  }]);
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html lang="en" ng-app="app">

<body ng-controller="main as vm">

  <select ng-model="vm.freqValue" ng-options="f.key as f.key for f in vm.freq" ng-change="vm.setMinMax()">
  <option value="">select</option>
</select>
  <input type="number" ng-min="{{vm.min}}" max="{{vm.max}}" ng-model="interval" />

</body>

</html>