从JSON文件填充mat-select选项

时间:2019-06-16 14:17:02

标签: json angular http

我试图从JSON文件中填充mat-select选项,但出现类似

的错误
  

SyntaxError:JSON中意外的令牌n在XMLHttpRequest.onLoad上JSON.parse()的位置8584处

我创建了一项获取列表的服务:

getCountries(): Observable<ICountry[]>{
    return this.http.get<ICountry[]>(this._url);
  }

我在这里尝试填充数组:

this._countriesService.getCountries()
      .subscribe(data => this.countries = data,
                error => console.log(error));

和html代码如下:

<mat-form-field>
<mat-label>Country From</mat-label>
<mat-select>
<mat-option>None</mat-option>
<mat-option *ngFor="let country of countries" [value]="country.name">{{country.name}}</mat-option>
</mat-select>
</mat-form-field>

JSON文件:

[ 
  {"name": "Afghanistan", "code": "AF"}, 
  {"name": "Åland Islands", "code": "AX"}, 
  {"name": "Albania", "code": "AL"}, 
  {"name": "Algeria", "code": "DZ"}, 
  {"name": "American Samoa", "code": "AS"}, 
  {"name": "AndorrA", "code": "AD"}, 
  {"name": "Angola", "code": "AO"}, 
  {"name": "Anguilla", "code": "AI"}, 
  {"name": "Antarctica", "code": "AQ"}, 
  {"name": "Antigua and Barbuda", "code": "AG"}, 
  {"name": "Argentina", "code": "AR"}, 
  {"name": "Armenia", "code": "AM"}, 
  {"name": "Aruba", "code": "AW"}, 
  {"name": "Australia", "code": "AU"}, 
  {"name": "Austria", "code": "AT"}, 
  {"name": "Azerbaijan", "code": "AZ"}
]

我需要显示所有名称以进行垫选择。 谢谢。

0 个答案:

没有答案