@angular-material-extensions/select-country

中文文档
17.0.0发布于 2023/11/19MITTypeScriptES Module近 30 天下载 7409

Angular Material component that allow users to select a country or nationality

原始使用文档
文档目录22 节
  1. Built by and for developers :heart:
  2. Table of Contents
  3. Demo
  4. Library's components
  5. Dependencies
  6. Installation
  7. 1. Install via ng add . (Recommended)
  8. 2. Install via npm . (Alternative)
  9. Import the library
  10. API
  11. <mat-select-country> used to display the main component - s…
  12. Usage
  13. option 1 with Reactive Forms
  14. option 2 with Reactive Forms
  15. Predefine your countries to load
  16. Improve performance - use the itemsLoadSize property
  17. Change language dynamically - use the language property
  18. Run Demo App Locally
  19. Other Angular Libraries
  20. Who is using this library?
  21. Support
  22. License

angular-material-extensions's logo

@angular-material-extensions/select-country - Angular Material component that allow users to select a country or nationality with an autocomplete feature

npm version npm demo docs: typedoc Join the chat at https://gitter.im/angular-material-extensions/Lobby Build Status codecov Greenkeeper Badge license

@angular-material-extensions/select-country demonstration

@angular-material-extensions/select-country demonstration

Built by and for developers :heart:

Do you have any question or suggestion ? Please do not hesitate to contact us! Alternatively, provide a PR | open an appropriate issue here

If you like this project, support angular-material-extensions by starring :star: and sharing it :loudspeaker:

Table of Contents

Demo

View all the directives and components in action at https://angular-material-extensions.github.io/select-country

Library's components

  • <mat-select-country> used to display the main component

Dependencies

  • Angular developed and tested with 17.x

Installation

If Angular Material Design is not setup, just run ng add @angular/material learn more

Now add the library via the angular schematics

ng add @angular-material-extensions/select-country

2. Install via npm. (Alternative)

Install peer dependencies

npm i svg-country-flags -s

then update your angular.json like below (svg-country-flags)

"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:

npm install --save @angular-material-extensions/select-country

Import the library

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:

import { MatSelectCountryModule } from "@angular-material-extensions/select-country";
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

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:

API

<mat-select-country> used to display the main component - see the demo examples

optionbindtypedefaultdescription
valueInput()Country-the selected country (pass an empty Country object with alpha2 code populated to do alpha2 lookup)
appearanceInput()MatFormFieldAppearance-Possible appearance styles for mat-form-field ('legacy', 'standard', 'fill' or 'outline')
countriesInput()Country[]All countries stored in the libCountries that should be loaded - predefine the countries that you only need!
labelInput()boolean-mat-form-field label's text
itemsLoadSizeInput()number-the number of countries that should be fetched --> improves the performance
placeHolderInput()boolean-input placeholder text
disabledInput()boolean-Whether the component is disabled
loadingInput()boolean-Whether the component is loading
nullableInput()boolean-Whether the component is able to emit null
readonlyInput()boolean-Whether the component is read only
tabIndexInput()number | string-Whether the component can be focused, and where it participates in sequential keyboard navigation
showCallingCodeInput()booleanfalseWhether the component to show the country's calling code in the label and selection
classInput()string-Class attribute apply style to input text or validation ignore (optional)
languageInput()string-the language, if not specified MatSelectCountryModule.forRoot('XX') will be used (optional)
nameInput()string'country'the attribute name of the input element
autocompleteInput()string-the attribute autocomplete of the input element, to avoid suggestion of some browsers put 'no'
onCountrySelectedOutput()EventEmitter<Country>-emits the selected country as object (see the interface below)
interface Country {
  name: string;
  alpha2Code: string;
  alpha3Code: string;
  numericCode: string;
  callingCode: string;
}

Usage

add the <mat-select-country> element to your template:

<mat-select-country>
</mat-select-country>

@angular-material-extensions/select-country demonstration

@angular-material-extensions/select-country demonstration

@angular-material-extensions/select-country demonstration

Use the library with reactive forms

option 1 with Reactive Forms

<mat-select-country appearance="outline"
                    label="Country"
                    [formControl]="countryFormControl"
                    (onCountrySelected)="onCountrySelected($event)">
</mat-select-country>

option 2 with Reactive Forms

<form [formGroup]="countryFormGroup">
    <div fxLayoutAlign="center">
      <mat-select-country appearance="outline"
                          label="Country"
                          class="className"
                          formControlName="country"
                          (onCountrySelected)="onCountrySelected($event)">
      </mat-select-country>
    </div>
  </form>
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);
  }
}

Predefine your countries to load

<mat-select-country appearance="outline"
                    label="Country"
                    [countries]="predefinedCountries"
                    (onCountrySelected)="onCountrySelected($event)">
</mat-select-country>
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:

@angular-material-extensions/select-country demonstration

Improve performance - use the itemsLoadSize property

<mat-select-country appearance="outline"
                    label="Country"
                    [itemsLoadSize]="5">
</mat-select-country>

only 5 countries will fetched!

Change language dynamically - use the language property

<mat-select-country appearance="outline"
                    label="Country"
                    [language]="languageSelected"
                    (onCountrySelected)="onCountrySelected($event)">
</mat-select-country>

Run Demo App Locally

Build the library

$ npm run build:lib

Serve the demo app

$ npm start

Other Angular Libraries


Who is using this library?

nahaus.de - Digital and Automated Real Estate Management

To put your project here, please drop an appropriate PR


Support

Built by and for developers :heart: we will help you :punch:


jetbrains logo

This project is supported by jetbrains with 1 ALL PRODUCTS PACK OS LICENSE incl. webstorm


License

Copyright (c) 2020-2024 Anthony Nahas. Licensed under the MIT License (MIT)

angular-material-extensions's logo

版本与发布记录

@angular-material-extensions/select-country 共发布 41 个版本,首次发布于 2019/11/15;过去 12 个月发布了 0 个版本。

版本发布日期距今
17.0.0latest2023/11/192 年前
16.0.02023/11/192 年前
8.1.02023/01/143 年前
8.0.02022/12/013 年前
7.1.02022/11/183 年前
7.0.12022/07/054 年前

关于 @angular-material-extensions/select-country 的常见问题

@angular-material-extensions/select-country 是什么?

Angular Material component that allow users to select a country or nationality

如何安装 @angular-material-extensions/select-country?

在项目目录运行 npm install @angular-material-extensions/select-country,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@angular/common(^17.x)、@angular/core(^17.x)、@angular/forms(^17.x)、@angular/platform-browser(^17.x)、@angular/cdk(~17.x)、@angular/material(~17.x)、rxjs(^7.x)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@angular-material-extensions/select-country 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@angular-material-extensions/select-country 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@angular-material-extensions/select-country 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=16。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@angular-material-extensions/select-country 可以商用吗?

@angular-material-extensions/select-country 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@angular-material-extensions/select-country 的下载量有多少?

根据 npm 官方下载统计,@angular-material-extensions/select-country 在 2026/10/06 – 2026/11/04 这 30 天内被下载 7,409 次,日均约 247 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@angular-material-extensions/select-country 还在维护吗?

最新版本 17.0.0 发布于 2023/11/19(2 年前);至今共发布 41 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/11/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。