【发布时间】:2020-08-10 04:43:06
【问题描述】:
在我的 Angular 应用程序中,我使用 api 来获取有关所选国家/地区的数据。
我无法让我的应用程序显示响应数据中语言部分的名称属性, 看起来像这样:
[{
"name": "Colombia",
"topLevelDomain": [".co"],
"alpha2Code": "CO",
"alpha3Code": "COL",
"callingCodes": ["57"],
"capital": "Bogotá",
"altSpellings": ["CO", "Republic of Colombia", "República de Colombia"],
"region": "Americas",
"subregion": "South America",
"population": 48759958,
"latlng": [4.0, -72.0],
"demonym": "Colombian",
"area": 1141748.0,
"gini": 55.9,
"timezones": ["UTC-05:00"],
"borders": ["BRA", "ECU", "PAN", "PER", "VEN"],
"nativeName": "Colombia",
"numericCode": "170",
"currencies": [{
"code": "COP",
"name": "Colombian peso",
"symbol": "$"
}],
"languages": [{
"iso639_1": "es",
"iso639_2": "spa",
"name": "Spanish",
"nativeName": "Español"
}],
"translations": {
"de": "Kolumbien",
"es": "Colombia",
"fr": "Colombie",
"ja": "コロンビア",
"it": "Colombia",
"br": "Colômbia",
"pt": "Colômbia"
},
"flag": "https://restcountries.eu/data/col.svg",
"regionalBlocs": [{
"acronym": "PA",
"name": "Pacific Alliance",
"otherAcronyms": [],
"otherNames": ["Alianza del Pacífico"]
}, {
"acronym": "USAN",
"name": "Union of South American Nations",
"otherAcronyms": ["UNASUR", "UNASUL", "UZAN"],
"otherNames": ["Unión de Naciones Suramericanas", "União de Nações Sul-Americanas", "Unie van Zuid-Amerikaanse Naties", "South American Union"]
}],
"cioc": "COL"
}]
我尝试过使用管道、嵌套的 *ngFor 循环,但在显示语言名称时没有运气。有什么建议吗?
在我的模板中,我使用以下 HTML 和插值来显示国家对象的名称: 如何使用类似的方法来访问响应数据语言中的名称属性?
<div>
<label>Country Capital:</label>
<p>{{ country.capital }} </p>
</div>
我的应用程序由 3 个模块、1 个父组件 (CountryComponent) 和两个子组件 (CountryListComponent) 和 (CountryDetailComponent) 组成。 使用 eventEmmiter 将数据从 List 组件发送到 Detail 组件。我对 Country 类型使用以下模型:
export interface Country {
name: string;
topLevelDomain?: string[];
alpha2Code?: string;
alpha3Code?: string;
callingCodes?: string[];
capital?: string;
altSpellings?: string[];
region?: string;
subregion?: string;
population?: number;
latlng?: number[];
demonym?: string;
area?: number;
gini?: number;
timezones?: string[];
borders?: string[];
nativeName?: string;
numericCode?: string;
currencies?: Currency[];
languages?: Language[];
translations?: Translation;
flag?: string;
regionalBlocs?: Regional[];
cioc?: string;
}
在我的列表组件中,我使用以下内容通过服务类获取数据并初始化 Country 类型的国家数组,并使用 eventEmmiter 将国家对象发送到 Detail 组件:
public countries: Country[];
getCountries(){
this.countryService
.getCountries()
.subscribe((data: any) => {
this.countries = data;
},
error => {console.log('error loading')});
}
@Output() selectedCountry = new EventEmitter<Country>();
列表组件模板:
<li class="list-group-item"
highlightDirective
style="cursor: pointer;
text-align: center;
font-family:'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;"
*ngFor="let country of countries | searchFilter: searchText : 'name'" (click)="onCountrySelected(country)">
{{ country.name }}
</li>
在 Detail 组件中我收到事件:
@Input() country: Country;
我正在尝试在详细模板中显示它:
<h2> {{ country.name }} </h2>
<br>
<div class="row" #countrySelected>
<div class="col-sm">
<label>Country Capital:</label>
<p>{{ country.capital }} </p>
<p>{{ country.languages.name }}</p>
</div>
<div>
我使用eventEmmiter将Country对象从List组件发送到使用父组件的Detail组件,父模板如下:
<div class="row"
style="padding-left: 20px;
padding-right: 20px;">
<div class="col-xs-5">
<app-country-list (selectedCountry)="childEventClicked($event)"></app-country-list>
</div>
<div class="col-xs-7">
<app-country-detail [country]="(selectedCountryEvent)"></app-country-detail>
</div>
</div>
【问题讨论】:
-
尝试将 JSON 存储在一个变量中,然后对其进行解析。你应该能够循环通过它。
-
请注意
interface Country中的属性languages是string[]。应该是any[]AFAIK -
感谢@Benny,我尝试了所有这些,我还分配了一个自定义类型,并为这些对象数组(如语言)创建了单独的接口。没有任何效果...
-
上传到 StackBlitz
-
@Benny,感谢您的回复,这里是项目的链接:stackblitz.com/edit/angular-fe7nzr
标签: javascript html angular