遵循这段代码:
<label class="checkbox-label">
<input name="radio1" type="radio"
ng-model="myModel.radio"
required ng-required="true"
value="1">
</label>
<label class="checkbox-label">
<input name="radio1" type="radio"
ng-model="myModel.radio"
required ng-required="true"
value="2">
</label>
<input type="number" name="result" class="form-control input-sm" ng-model="myModel.result"
required ng-required="true" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/" step="0.1" min="0" max="100">
我需要设置ng-pattern
&amp; step
根据radio1
选择接受一位小数(0.1)或两位小数(0.01)
任何人都知道如何处理这个问题?
更新 我已经创建了一个指令来执行此操作,但更改模式后不会触发验证(Angular 1.2)
app.directive('stepChange', function () {
return {
link: function link(scope, element, attr) {
element.bind('change', function () {
var el = angular.element(attr['target']);
el.attr('step', attr['step']);
el.attr('pattern', attr['pattern']);
el.val(attr['val']);
el.trigger();
});
}
}
});
答案 0 :(得分:0)
答案 1 :(得分:0)
这样做的关键是:
更改上述监视中的步骤值并使用范围变量
将其分配回来$scope.$watch('myModel.radio',function(a,b){ debugger if(a ==1){ $scope.step = "0.1"; }else if(a==2){ $scope.step = "0.01"; } });
请在plunkr中自行应用步骤逻辑/值。
https://plnkr.co/edit/BKTS5jDTRoRQomNzHSt2?p=preview
您也可以使ng-pattern动态相同(如步骤)
<input type="number" name="result" class="form-control input-sm"
ng-model="myModel.result" required ng-required="true"
ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/" step={{step}} min="0" max="100">