我在函数中有一个简单的订阅observable:
public MyGet () {
let results={};
this.http.get("myfile.json").map(res => res.json()).subscribe(
output => {
results=output.clients; // clients is the root of json file
},
/* etc */
json文件“myfile.json”是:
{
"clients" : [
{ "name":"X",
"age":"34" },
{ "name": "Y",
"age": "41" },
/* etc */
我希望“clients”作为MyGet函数中的参数:
public MyGet (json_root: any){
let results={};
this.http.get("myfile.json").map(res => res.json()).subscribe(
output => {
results=output.HERE; // HERE = json_root
},
/* etc */
所以我可以致电:
MyGet("clients")
我不明白如何在HERE中编写json_root
答案 0 :(得分:2)
You can split your function.
public MyGet (): Observable<any>{
return this._http.get("myfile.json").map(res => res.json());
}
....
private json_root: any;
public callAndSubscrive(json_key: string){
this.MyGet().subscribe(output => {this.json_root = output[json_key]; console.log(output)});
}