如何將資料存盤在 requestArray 上,然后使用它來修補它?
requestArray 在代碼上不可用
我試過了,但在修補它之前它給了我未定義的一行。
ngOnInit(): void {
this.getOneRequest(localStorage.getItem("xyz")); //returns id of the array
}
requestArray: any;
data={areaOfInterest:"",resourceDTOS:[{seniority:"",skillDTOS:[{skill:"",},],resourceNotes:"",},],};
//just a json
myForm: FormGroup;
constructor(private fb: FormBuilder, private requestService: RequestService) {
this.myForm = this.fb.group({
areaOfInterest: [""],
startDate: [""],
endDate: [""],
description: [""],
notes: [""],
resourceDTOS: this.fb.array([]),
});
this.setResourceDTOS();
console.log(this.requestArray); //returns undefined
this.myForm.patchValue(this.requestArray);
}
getOneRequest(id: string) {
return this.requestService.GetSingleRequest(id).subscribe((data) => {
console.log(data);
JSON.stringify(data);
this.requestArray = data;
});}
我能做些什么來潛在地解決這個問題?
uj5u.com熱心網友回復:
建構式方法總是在 ngOnInit 之前執行。此外,回應只能在 API 請求中使用。
要解決此問題,請將 API 請求移動到建構式中并在請求中呼叫 patchValue 函式:
新建構式:
constructor(private fb: FormBuilder, private requestService: RequestService) {
this.myForm = this.fb.group({
areaOfInterest: [""],
startDate: [""],
endDate: [""],
description: [""],
notes: [""],
resourceDTOS: this.fb.array([]),
});
this.setResourceDTOS();
this.requestService.GetSingleRequest(id).subscribe((data) => {
console.log(data);
JSON.stringify(data);
this.requestArray = data;
console.log(this.requestArray); //will no longer be undefined
this.myForm.patchValue(this.requestArray);
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/342732.html
標籤:有角的
上一篇:如何在自定義指令中使用屬性系結?
