在Angular 2表单中,我需要从http get call
检索名字和姓氏,提供user_id
,由先前http get call
检索到另一个终点。
我知道async http get calls
存在类似的问题,但这些都没有真正帮助我。
因此,第一个调用接收user_id
字段,而不向服务器提供任何内容:
/API/system/session/unusedid
返回此 - >
{user_name":"rgar003","session_id":"0635400936","is_technical_user":"false","user_id":"403"
拥有此user_id (403)
,我可以调用此其他端点并检索其userName
和lastName
:
/API/identity/user/403 //403 or whatever the user_id is
返回此 - >
{"job_title":"dsf","userName":"rgar003","lastname":"Garcia","firstname":"Robert"}
因此,我需要在禁用的表单输入中提供名字+姓氏。现在我只提供user_id
。
带有禁用字段的模板,其中显示名称和lastmane:
<div class="col-sm-5">
<label for="petitioner">Petitioner</label>
<input [(ngModel)]="data.petitioner" type="text" class="form-control" name="petitioner" disabled>
</div>
home
组件,从中调用函数从后端加载请愿者user_id
:
constructor(private workflowService: WorkflowService, private parametersService: UrlParametersService, private _ref: ChangeDetectorRef) {
this.formMode = parametersService.formMode;
// origin: /API/system/session/unusedId
this.workflowService.loadPetitioner().subscribe(
res => {
this.data.petitioner= res;
}
);
包含该功能的workflow.service
:
constructor(private http: Http, private parametersService: UrlParametersService, private router: Router, private _configurations: ConfigurationsService) {}
public loadPetitioner(): Observable<string> {
return this.http.get("/API/system/session/unusedId", {withCredentials: true}).map(value => value.json().user_name);
}
我尝试了不同的链接方式,但都失败了。
如果我致电/API/identity/user/403
,那么提供user_id
硬编码,那么获得名字+姓氏就行了。像这样:
getPetitionerFirstNamePlusLastName(): Observable<string>{
return this.http.get("/API/identity/user/403", {withCredentials: true}).map(value =>(value.json().firstname + " " + value.json().lastname));
}
我似乎在这里存在异步问题,但即使在检查了其他类似问题之后我也无法解决它。
答案 0 :(得分:0)
试一试
this.workflowService.loadPetitioner().concatMap( res => {
let id = res;
return this.getPetitionerFirstNamePlusLastName(id);
}).subscribe( res => {
// Do stuff with the user
});
在getPetitionerFirstNamePlusLastName中添加 id 作为参数
getPetitionerFirstNamePlusLastName(id:number): Observable<string>{
return this.http.get("/API/identity/user/" + id, {withCredentials: true}).map(value =>(value.json().firstname + " " + value.json().lastname));
}