Для меня документация i18n-iso-countries немного сбивает с толку. У меня есть проект Angular 7, и я просто хочу отображать все страны для пользователя в регистрационной форме в зависимости от его текущего языка.
Итак, я знаю, что i18nIsoCountries.getNames("en")
дает мне все имена и что-то вроде вывода JSON. Текущий язык я могу легко получить с помощью this.translateService.currentLang
, но на данный момент у меня есть статический собственный письменный файл JSON только для языков на английском языке, и я отображаю их в своей форме.
Я также хочу отфильтровать страны, в которые я могу написать Ger, и получить предложение Германии. На данный момент это реализовано и работает, но я не знаю, как это сделать с библиотекой.
Компонент регистрации:
import country from '../../assets/countriesJson.json';
export interface Country {
name: string;
code: string;
}
export class RegistrationComponent implements OnInit {
@ViewChild('stepper') stepper: MatStepper;
filteredCountries: Observable<Country[]>;
countryList: Country[] = country.countries;
ngOnInit() {
this.filteredCountries = this.personalForm.controls['country'].valueChanges
.pipe(
startWith(''),
map(value => this._filterCountries(value))
);
}
private _filterCountries(value: string): Country[] {
const filterValue = value.toLowerCase();
return this.countryList.filter(country => country.name.toLowerCase().indexOf(filterValue) === 0);
// const filterValue = value.toLowerCase(); Variante für Suche bei dem Begriff nur enthalten ist Ger -> AlGERia
// return this.countryList.filter(option => option.name.toLowerCase().includes(filterValue));
}
CountryJson (краткая форма):
{
"countries": [
{"name": "Afghanistan", "code": "AF"},
{"name": "Åland Islands", "code": "AX"} ]
}
HTML:
<mat-form-field class="field-sizing">
<input matInput required placeholder="{{ 'REGISTRATION.COUNTRY' | translate }}" name="country"
id="country" [matAutocomplete]="auto" formControlName="country"
[ngClass]="{ 'is-invalid': g.country.touched && g.country.errors }" />
<mat-autocomplete autoActiveFirstOption #auto="matAutocomplete">
<mat-option *ngFor="let country of filteredCountries | async" [value]="country.name">
{{country.name}}
</mat-option>
</mat-autocomplete>
<mat-error class="invalid-feedback"
*ngIf="g.country.touched && g.country.errors && g.country.errors.required">
{{ 'REGISTRATION.COUNTRY' | translate }} {{ 'VALIDATION.REQUIRED' | translate }}
</mat-error>
</mat-form-field>
Так что идея в голове - как-то временно сохранить страны как какие-то данные с i18nIsoCountries.getNames("en")
getNames(lang: string): i18nIsoCountries.LocalizedCountryNames
просто дает ее как строку, даже если ее вывод представляет собой пару ключ-значение. - person CptDayDreamer   schedule 27.05.2019