Angular Material component that allow users to select a country or nationality
npm install @angular-material-extensions/select-country
height="256px" width="256px" style="text-align: center;"
src="https://cdn.jsdelivr.net/gh/angular-material-extensions/select-country@master/assets/angular-material-extensions-logo.svg">








src="https://raw.githubusercontent.com/angular-material-extensions/select-country/HEAD/assets/v0.2.0/select-country.gif">
src="https://raw.githubusercontent.com/angular-material-extensions/select-country/HEAD/assets/v0.2.0/select-country.png">
If you like this project, support angular-material-extensions
by starring :star: and sharing it :loudspeaker:
View all the directives and components in action at https://angular-material-extensions.github.io/select-country
used to display the main component---
17.x---
If Angular Material Design is not setup, just run ng add @angular/material learn more
Now add the library via the angular schematics
``shell`
ng add @angular-material-extensions/select-country
Install peer dependencies
`shell`
npm i svg-country-flags -s
then update your angular.json like below (svg-country-flags)
`json`
"assets": [
"src/favicon.ico",
"src/assets",
{
"glob": "*/",
"input": "./node_modules/svg-country-flags/svg",
"output": "src/assets/svg-country-flags/svg"
}
],
Now install @angular-material-extensions/select-country via:
`shell`
npm install --save @angular-material-extensions/select-country
If you installed the library via angular schematics, you can skip this step
Once installed you need to import the main module and the HttpClientModule:
`js`
import { MatSelectCountryModule } from "@angular-material-extensions/select-country";
`typescript
import { MatSelectCountryModule } from '@angular-material-extensions/select-country';
import {HttpClientModule} from '@angular/common/http';
@NgModule({
declarations: [AppComponent, ...],
imports: [
MatSelectCountryModule.forRoot('de'), // you can use 'br' | 'de' | 'en' | 'es' | 'fr' | 'hr' | 'hu' | 'it' | 'nl' | 'pt' --> MatSelectCountrySupportedLanguages
HttpClientModule, ...],
bootstrap: [AppComponent]
})
export class AppModule {
}
`
in other modules
`typescript
import { MatSelectCountryModule } from '@angular-material-extensions/select-country';
import {HttpClientModule} from '@angular/common/http';
@NgModule({
declarations: [AppComponent, ...],
imports: [MatSelectCountryModule, HttpClientModule, ...],
bootstrap: [AppComponent]
})
export class AppModule {
}
`
Other modules in your application like for lazy loading import MatSelectCountryModule into your feature module:
| option | bind | type | default | description |
| :---------------- | :--------: | :----------------------: | :-----------------------------: | :------------------------------------------------------------------------------------------------- |
| value | Input() | Country | - | the selected country (pass an empty Country object with alpha2 code populated to do alpha2 lookup) |Input()
| appearance | | MatFormFieldAppearance | - | Possible appearance styles for mat-form-field ('legacy', 'standard', 'fill' or 'outline') |Input()
| countries | | Country[] | All countries stored in the lib | Countries that should be loaded - predefine the countries that you only need! |Input()
| label | | boolean | - | mat-form-field label's text |Input()
| itemsLoadSize | | number | - | the number of countries that should be fetched --> improves the performance |Input()
| placeHolder | | boolean | - | input placeholder text |Input()
| disabled | | boolean | - | Whether the component is disabled |Input()
| loading | | boolean | - | Whether the component is loading |Input()
| nullable | | boolean | - | Whether the component is able to emit null |Input()
| readonly | | boolean | - | Whether the component is read only |Input()
| tabIndex | | number \| string | - | Whether the component can be focused, and where it participates in sequential keyboard navigation |Input()
| showCallingCode | | boolean | false | Whether the component to show the country's calling code in the label and selection |Input()
| class | | string | - | Class attribute apply style to input text or validation ignore (optional) |Input()
| language | | string | - | the language, if not specified MatSelectCountryModule.forRoot('XX') will be used (optional) |Input()
| name | | string | 'country' | the attribute name of the input element |Input()
| autocomplete | | string | - | the attribute autocomplete of the input element, to avoid suggestion of some browsers put 'no' |Output()
| onCountrySelected | | EventEmitter | - | emits the selected country as object (see the interface below) |
`typescript`
interface Country {
name: string;
alpha2Code: string;
alpha3Code: string;
numericCode: string;
callingCode: string;
}
add the element to your template:
`html`
src="https://raw.githubusercontent.com/angular-material-extensions/select-country/HEAD/assets/v0.2.0/example_full.png">
src="https://raw.githubusercontent.com/angular-material-extensions/select-country/HEAD/assets/v0.2.0/example3.png">
src="https://raw.githubusercontent.com/angular-material-extensions/select-country/HEAD/assets/v0.2.0/example2.png">
#### Use the library with reactive forms
`html`
[formControl]="countryFormControl"
(onCountrySelected)="onCountrySelected($event)">
`html`
`typescript
import {Component,OnInit} from '@angular/core';
import {FormControl} from '@angular/forms';
import {Country} from '@angular-material-extensions/select-country';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit{
title = 'select-country';
countryFormControl = new FormControl();
countryFormGroup: FormGroup;
constructor(private formBuilder: FormBuilder) {
angulartics2GoogleAnalytics.startTracking();
}
ngOnInit(): void {
this.countryFormGroup = this.formBuilder.group({
country: []
});
this.countryFormGroup.get('country').valueChanges
.subscribe(country => console
.log('this.countryFormGroup.get("country").valueChanges', country));
this.countryFormControl.valueChanges
.subscribe(country => console
.log('this.countryFormControl.valueChanges', country));
}
onCountrySelected($event: Country) {
console.log($event);
}
}
`
`html`
[countries]="predefinedCountries"
(onCountrySelected)="onCountrySelected($event)">
`typescript
import {Country} from '@angular-material-extensions/select-country';
predefinedCountries: Country[] = [
{
name: 'Germany',
alpha2Code: 'DE',
alpha3Code: 'DEU',
numericCode: '276',
callingCode: '+49'
},
{
name: 'Greece',
alpha2Code: 'GR',
alpha3Code: 'GRC',
numericCode: '300',
callingCode: '+30'
},
{
name: 'France',
alpha2Code: 'FR',
alpha3Code: 'FRA',
numericCode: '250',
callingCode: '+33'
},
{
name: 'Belgium',
alpha2Code: 'BE',
alpha3Code: 'BEL',
numericCode: '056',
callingCode: '+32'
}
];
`
Result:
src="https://raw.githubusercontent.com/angular-material-extensions/select-country/HEAD/assets/v2.1.0/predefined.png">
`html`
[itemsLoadSize]="5">
only 5 countries will fetched!
html
label="Country"
[language]="languageSelected"
(onCountrySelected)="onCountrySelected($event)">
`$3
Build the library
`bash
$ npm run build:lib
`Serve the demo app
`bash
$ npm start
``nahaus.de - Digital and Automated Real Estate Management
To put your project here, please drop an appropriate PR
-----
Built by and for developers :heart: we will help you :punch:
---
This project is supported by jetbrains with 1 ALL PRODUCTS PACK OS LICENSE incl. webstorm
---
Copyright (c) 2020-2024 Anthony Nahas. Licensed under the MIT License (MIT)
height="92px" width="92px" style="text-align: center;"
src="https://cdn.jsdelivr.net/gh/angular-material-extensions/select-country@master/assets/badge_made-in-germany.svg">