[英]populate FormBuilder from a service in Angular2
我有一個通過服務填充的Angular2模型。 我想使用此模型來填充我的表單(從FormBuilder
構建),以便我的用戶可以編輯數據。
這是我當前正在執行的操作,但是對於模型中存在但未在表單中公開的字段,我會遇到錯誤。
...
ngOnInit(): void {
this.buildForm();
this.get('1');
}
get(id: string) {
this.myModelsService.get(id)
.subscribe(
d => {
this.myModel = d;
this.myForm.setValue(d);
},
error => console.log(error)
);
}
這行得通,但是出現諸如Cannot find form control with name: incidentTimeStamp
類的錯誤。
我應該首先刪除我知道沒有相應表單控件的屬性嗎? 還是有更好的方法來做我想做的事情?
能夠填充字段以進行編輯似乎是一個非常基本的構建塊,並且這似乎比應做的要困難得多。
解決方案是使用FormGroup.patchValue()
FormGroup.setValue()
不嚴格把關,同時FormGroup.patchValue()
沒有。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.