Skip to content

Instantly share code, notes, and snippets.

@osahner
Created April 21, 2017 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osahner/f7c6edf08cecff45361db1cefca2094f to your computer and use it in GitHub Desktop.
Save osahner/f7c6edf08cecff45361db1cefca2094f to your computer and use it in GitHub Desktop.
angular2 bootstrap4 country select menu
import { Component, EventEmitter, Input, Output } from '@angular/core';
import * as iso3166 from 'iso-3166-2/iso3166.min';
@Component({
selector: 'app-country-select',
template: `
<select name="theme" [class]="'form-control' + (size ? ' form-control-' + size : '')"
[ngModel]="countryIsoCode" (ngModelChange)="change($event)">
<option *ngFor="let country of countries" [ngValue]="country.value">{{country.display}}</option>
</select>
`
})
export class CountrySelectComponent {
@Input()
public countryIsoCode: string;
@Input()
public size: 'sm' | 'lg';
@Output()
public countryIsoCodeChange = new EventEmitter();
public countries: any[] = [];
constructor() {
for (const key of Object.keys(iso3166.data)) {
this.countries.push({ display: iso3166.data[key].name, value: key.toLowerCase() });
}
this.countries.sort((a: any, b: any) => a.display.localeCompare(b.display));
}
public change(newValue: string): void {
this.countryIsoCode = newValue;
this.countryIsoCodeChange.emit(newValue);
}
}
@osahner
Copy link
Author

osahner commented Apr 21, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment