src/country-picker.component.ts
changeDetection | ChangeDetectionStrategy.OnPush |
selector | country-picker |
template |
|
Properties |
Methods |
Inputs |
constructor(_countryPickerService: CountryPickerService, _cdr: ChangeDetectorRef)
|
|||||||||
Defined in src/country-picker.component.ts:25
|
|||||||||
Parameters :
|
classes | |
Type : {}
|
|
Default value : ['form-control', 'form-control-sm']
|
|
Defined in src/country-picker.component.ts:22
|
flag | |
Type : boolean
|
|
Default value : false
|
|
Defined in src/country-picker.component.ts:19
|
setName | |
Type : string
|
|
Default value : 'name.common'
|
|
Defined in src/country-picker.component.ts:21
|
setValue | |
Type : string
|
|
Default value : 'cca3'
|
|
Defined in src/country-picker.component.ts:20
|
Public getName | ||||||
getName(obj: ICountry)
|
||||||
Defined in src/country-picker.component.ts:56
|
||||||
Parameters :
Returns :
string
|
Public getValue | ||||||
getValue(obj: ICountry)
|
||||||
Defined in src/country-picker.component.ts:52
|
||||||
Parameters :
Returns :
string
|
Public ngOnInit |
ngOnInit()
|
Defined in src/country-picker.component.ts:34
|
Returns :
void
|
Public baseUrl |
Type : string
|
Defined in src/country-picker.component.ts:25
|
Public countries |
Type : ICountry[]
|
Default value : []
|
Defined in src/country-picker.component.ts:24
|
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnInit } from '@angular/core';
import { ICountry } from './country.interface';
import { CountryPickerService } from './country-picker.service';
@Component({
selector: 'country-picker',
template: `
<select [class]="classes">
<option *ngFor="let c of countries" [value]="getValue(c)">
<img *ngIf="flag" [src]="baseUrl + c.cca3.toLowerCase() + '.svg'">{{ getName(c) }}
</option>
</select>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CountryPickerComponent implements OnInit {
@Input() public flag = false;
@Input() public setValue = 'cca3';
@Input() public setName = 'name.common';
@Input() public classes = ['form-control', 'form-control-sm'];
public countries: ICountry[] = [];
public baseUrl: string;
constructor(
private _countryPickerService: CountryPickerService,
private _cdr: ChangeDetectorRef,
) {
this.baseUrl = _countryPickerService.getBaseUrl() + 'data/';
}
public ngOnInit(): void {
this._countryPickerService.getCountries()
.subscribe(countries => {
this.countries = countries.sort((a: ICountry, b: ICountry) => {
const na = this.getName(a);
const nb = this.getName(b);
if (na > nb) {
return 1;
}
if (na < nb) {
return -1;
}
return 0;
});
this._cdr.markForCheck();
});
}
public getValue(obj: ICountry): string {
return this.setValue.split('.').reduce((o, i) => o[i], obj);
}
public getName(obj: ICountry): string {
return this.setName.split('.').reduce((o, i) => o[i], obj);
}
}