Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular http.get()传递ID_Angular_Typescript_Http Get - Fatal编程技术网

Angular http.get()传递ID

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

我试图通过typescript中的http.get()从URL调用中获取详细信息。检索此详细信息的URL端点是一种
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())
        );
}