Angular http.get()传递ID
我试图通过typescript中的http.get()从URL调用中获取详细信息。检索此详细信息的URL端点是一种Angular http.get()传递ID,angular,typescript,http-get,Angular,Typescript,Http Get,我试图通过typescript中的http.get()从URL调用中获取详细信息。检索此详细信息的URL端点是一种localhost:8001\character\1234567890,其中number是字符的ID 如果我在浏览器上测试,URL将正确返回所有数据 我的问题是typescript函数调用。在HTML前端,我将函数调用为: <a href="#{{character.id}}" (click)="characterDetail(character.id)">Read mo
localhost:8001\character\1234567890
,其中number是字符的ID
如果我在浏览器上测试,URL将正确返回所有数据
我的问题是typescript函数调用。在HTML前端,我将函数调用为:
<a href="#{{character.id}}" (click)="characterDetail(character.id)">Read more</a>
来自第一个函数的console.log(res)
不会打印任何内容characterId
和url都正确,但不知道不检索字符详细信息的原因
第二部分是在模式窗口上显示数据检索
编辑:这里有更多的代码:
private characterDetailApiUrl = 'http://localhost:8001/character/';
....
//To save the character details from Http get.
asyncCharDetail: Observable<string[]>;
characterDetail(characterId: number) {
console.log('Character detail for ID ' + characterId);
this.loading = true;
this.asyncCharDetail = this.getCharacterDetail(characterId)
.do(res => {
console.log(res);
this.loading = false;
})
.map(res => res.results);
console.log(this.asyncCharDetail);
}
getCharacterDetail(characterId: number) {
return this.http.get(this.characterDetailApiUrl + characterId )
.map((res: Response) => res.json());
}
private characterDetailApiUrl='2!'http://localhost:8001/character/';
....
//从Http get保存字符详细信息。
细节:可观察;
characterDetail(characterId:number){
log('ID'的字符详细信息+characterId);
这是。加载=真;
this.asyncCharDetail=this.getCharacterDetail(characterId)
.do(res=>{
控制台日志(res);
这一点:加载=错误;
})
.map(res=>res.results);
log(this.asynchCharDetail);
}
getCharacterDetail(characterId:number){
返回this.http.get(this.characterDetailAPIRL+characterId)
.map((res:Response)=>res.json());
}
在Html前端文件上:
<a href="" (click)="characterDetail(character.id); $event.preventDefault()">Read more</a>
返回this.http.get(this.characterdetailapirl+characterId,{})
只要URL就可以了。你能检查一下你删除,{}
后得到了什么吗?因此返回this.http.get(this.characterdetailapirl+characterId)
如果您需要完整响应,请尝试将
{}
替换为{observe:'response'}
您必须订阅Observable才能执行某些操作
characterDetail(characterId: number) {
this.getCharacterDetail(characterId)
.do(res => {
console.log(res);
})
.map(res => res.results)
.subscribe(data => {
//...do something with data
});
}
我已经从2变为1,现在我从角色那里得到了一些数据。现在作为@CornelC建议工作
getCharacterDetail(characterId: number) {
return this.http.get(this.characterDetailApiUrl + characterId)
.subscribe(res =>
console.log(res.json())
);
}
现在我从前端调用函数,但需要显示一个包含检索数据的模式窗口Mmmm,我定义了
asyncCharDetail:Observable代码>如果删除{}
的结果相同。。。将编辑代码以提供有关问题的更多信息您获取数据是因为您订阅了(如我所建议的),而不是因为您从2个方法更改为1个方法
getCharacterDetail(characterId: number) {
return this.http.get(this.characterDetailApiUrl + characterId)
.subscribe(res =>
console.log(res.json())
);
}