【发布时间】:2019-07-30 14:32:53
【问题描述】:
基本上我需要通过传递 id 来执行 http 请求。收到响应后,我需要将值分配给表单字段。
我尝试过使用 setValue 和 patchValue。但是没用
spService
itemData = new ItemData()
getItembyID(itemID) {
return this.http.get<Object>(this.baseUrl +"items("+itemID+")")
.pipe(map ((response : Response) => {
const data = response['d']
return data
})
).subscribe((data : Response) => {
this.setItemData(data)
})
}
setItemData(data) {
this.itemData.requestno = data.Request_x0020_Number
this.itemData.requestid = data.Request_x0020_ID
edit-form.component.ts
constructor(private spService : SharepointService,
private formBuilder : FormBuilder)
ngOnInit() {
this.route.params.subscribe((param : Params)=> {
const id = param['ID']
this.spService.getItemByID(ID)
})
this.itemData = this.sharepointService.itemData
// In console, I can see the details of the itemData but not able to set to the formcontrols.
this.editForm = this.formBuilder.group({
RequestNo : [''],
RequestId : ['']
})
this.editForm.patchValue({
RequestNo : this.itemData.requestno,
RequestId : this.itemData.requestid
})
} // End of OnInit
edit-form.component.html
<form [formGroup]= "editForm">
<label> DefaultRequestNo </label>
<input type="text" formControlName="RequestNo">
<label> DefaultRequestID </label>
<input type="text" formControlName="RequestId">
</form>
【问题讨论】:
-
需要查看 SharePointService 以及 itemData 的填充方式/时间。如果它是同步可用的,那么您在此处所拥有的应该可以工作(尽管 IMO 有一种更简洁的方法来完成它)
-
在 SharePoint 服务中,我有 itemData、getItemByID、setItemData 方法。我在顶部添加了 SharePoint 服务代码
标签: angular angular7 angular-reactive-forms