Angular 从角度组件中的Json响应中提取数据
基本上我有一个角度组件,它从WEBApi得到如下响应Angular 从角度组件中的Json响应中提取数据,angular,asp.net-web-api,Angular,Asp.net Web Api,基本上我有一个角度组件,它从WEBApi得到如下响应 export class FetchDataComponent implements OnInit { constructor(private _httpService: Http) { } City: string[] = []; ngOnInit() { // city and country this._httpService.get('/api/City').subs
export class FetchDataComponent implements OnInit {
constructor(private _httpService: Http) { }
City: string[] = [];
ngOnInit() {
// city and country
this._httpService.get('/api/City').subscribe(values => {
this.City = values.json() as string[];
});
}
}
我在这里得到的答复是[“滑铁卢”,“加拿大”]
如何从上面的回答中提取城市滑铁卢?根据要求,我需要在这里做
我试图将滑铁卢存储在单独的变量中,将加拿大存储在单独的变量中。我不确定我是否正确理解您的问题。如果你想从你的
城市
对象中得到“滑铁卢”,你可以使用
响应是字符串数组:
City: string[] = [];
var1: string;
var2: string;
ngOnInit() {
this._httpService.get('/api/City').subscribe(values => {
this.City = values.json() as string[];
this.var1 = this.City[0];
this.var2 = this.City[1];
});
}
City: string[] = [];
var1: string;
var2: string;
ngOnInit() {
this._httpService.get('/api/City').subscribe(values => {
this.City = values.json() as string[];
this.var1 = this.City[0];
this.var2 = this.City[1];
});
}
City: string[] = [];
ngOnInit() {
this._httpService.get('/api/City').subscribe(values => {
this.City = values.json() as string[];
var city = this.City.find(x => x === 'Waterloo'(please input city you want to get));
});
}