我有两个输入字段:select
Yes/No
和number
输入。
当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>
答案 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
}
}
});