我正在使用 angular 2 formbuilder 创建一个表单,我想根据单选框输入更改有条件地验证输入字段。当我单击百分比单选框时,应该验证百分比输入。当我单击金额单选框时,它应该验证金额输入字段。
HTML 代码
(我正在使用材质输入组件)
<md-radio-group formControlName="splitType" (click)="splitTypeClicked()">
<md-radio-button value="amount">Amount</md-radio-button>
<md-radio-button value="percentage">Percentage</md-radio-button>
</md-radio-group>
<input mdInput placeholder="Amount" formControlName="amount" >
<input mdInput placeholder="Percentage" formControlName="percentage" >
Angular 2 组件
constructor(private fb: FormBuilder){}
ngOnInit() {
this.splitChargeForm = this.fb.group({
splitType: ['', Validators.required],
amount: [''], //validate when splitType = amount
percentage: [''] //validate when splitType = percentage
});