我想尝试从 json 文件 url 加载国家:https : //raw.githubusercontent.com/sagarshirbhate/Country-State-City-Database/master/Contries.json 到 angular2-multiselect。下面是我的代码getCountries(){this.country.allCountries().subscribe( data2 => { this.countryInfo = data2.Countries.CountryName; console.log('Data:', this.countryInfo); }, err => console.log(err), () => console.log('complete') )}所以 getcountry 函数从我用这里的 url 创建的服务获取数据就是服务 export class CountriesService { url: string = "https://raw.githubusercontent.com/sagarshirbhate/Country-State-City- Database/master/Contries.json"; constructor(private http: HttpClient) { } allCountries(): Observable<any>{ return this.http.get(this.url); } }此显示数据:在控制台日志中未定义。请帮我解决
1 回答
九州编程
TA贡献1785条经验 获得超4个赞
使用以下代码获取CountryName。
getCountries(){
this.countryInfo = [];
this.country.allCountries().
subscribe(
data2 => {
data2.Countries.forEach(element => {
this.countryInfo.push(element.CountryName);
});
console.log('Data:', this.countryInfo);
},
err => console.log(err),
() => console.log('complete')
)
}
添加回答
举报
0/150
提交
取消