从 json 文件填充国家到 angular2-multiselect

我想尝试从 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);

 }

 }

此显示数据:在控制台日志中未定义。请帮我解决


aluckdog
浏览 82回答 1
1回答

九州编程

使用以下代码获取CountryName。getCountries(){&nbsp;this.countryInfo = [];this.country.allCountries().subscribe(&nbsp; &nbsp; data2 => {&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; data2.Countries.forEach(element => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.countryInfo.push(element.CountryName);&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; console.log('Data:', this.countryInfo);&nbsp; &nbsp; },&nbsp; &nbsp; err => console.log(err),&nbsp; &nbsp; () => console.log('complete')&nbsp;)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript