有沒有什麼辦法來更新它的控制後宣佈,像更新校驗在FormControl
this.input = new FormControl('', Validators.required)
this.form = this.formBuilder.group({
input = this.input
})
this.input.update('', Validators.maxlength(20))
有沒有什麼辦法來更新它的控制後宣佈,像更新校驗在FormControl
this.input = new FormControl('', Validators.required)
this.form = this.formBuilder.group({
input = this.input
})
this.input.update('', Validators.maxlength(20))
您可以使用setValidators
如果您要設置新的驗證器(S)在稍後的時間點,您可能還想要更新該值和有效性,可以使用updateValueAndValidity
運行。這裏有一個簡單的例子:
this.myForm.controls.input.setValidators([Validators.required,
Validators.minLength(4)]);
this.myForm.controls.input.updateValueAndValidity();
如果要更新字段值,您可以按上述使用patchValue
。
可以更新FormControl或FormGroup與setValue
方法,或patchValue方法。在你的情況下,最好使用setValue
。
做什麼patchValue
是,如果你想更新某個對象的形式,而該對象包含比形式(這意味着某些屬性不窗體上存在)更多的屬性,與patchValue只會得到其數值存在於窗體上,在這種情況下,如果使用setValue,則會出現錯誤。欲瞭解更多類似這樣的問題,它總是最好的,如果你使用的文檔(其中有方式更多的細節比我能在這裏解釋)
https://angular.io/docs/ts/latest/api/forms/index/FormControl-class.html
我使用setValidators,因爲你建議,謝謝 –
不客氣,很高興我可以幫助! :) – Alex