AngularJS:根据其他字段有条件地设置min

时间:2016-08-16 03:36:27

标签: angularjs

我有两个输入字段:select Yes/Nonumber输入。 当select 时,number被禁用并将其设置为0.但当select 时,我我想把它设置为最低1。

<body ng-app="KnobDemoApp">
  <div ng-controller="knobCtrl as knob">
    <label>Yes/No:</label>
    <select ng-model="knob.yesno" name="yesnoName" ng-options="x for x in yes_no">
        <option value="" disabled>Select One</option>
    </select>
    <br>
    <label>Number:</label>
    <input type="number" ng-model="numyesno" name="numName"
    ng-disabled="knob.yesno != 'Yes'"
    min="if knob.yesno == 'Yes' then 1 else 0"></input>
    <br><br>
    <span> Value: </span>{{knob.yesno}} <br>
    <span> Check: </span>{{knob.yesno != 'Yes'}}
  </div>
</body>
<script>
var app = angular.module('KnobDemoApp', [])
app.controller('knobCtrl', function ($scope) {
  $scope.yes_no = ["Yes", "No"];
});
</script>

1 个答案:

答案 0 :(得分:1)

您可以使用ngChange获取current的{​​{1}}值,然后将ngModel设置为0,等同于“否”

input
angular
  .module('KnobDemoApp', [])
  .controller('knobCtrl', function($scope) {
    $scope.setValue = setValue;
    $scope.yes_no = ["Yes", "No"];

    function setValue() {
      if ($scope.knob.yesno === 'No') {
        $scope.numyesno = 0;
      } else {
        $scope.numyesno = 1
      }
    }
  });