Git Product home page Git Product logo

angular-enum-list's Introduction

angular-enum-list

This package can be used for transformation enum's values to array where names are your own path with namespace and dictionary name to localizate word.

Installation

1. Install package npm install angular-enum-list --save

2. Import AngularEnumListModule to AppModule

import { AngularEnumListModule } from 'angular-enum-list';

@NgModule({
  bootstrap: [ AppComponent ],
  declarations: [   
    AppComponent
  ],
  import: [
    AngularEnumListModule.forRoot('enums') // first parameter is localization namespace name,
                                           // second parameter is separator, default ':'
  ]
})
export class AppModule {}

You can configurate name of localize namespace for global context and separator which break namespace and path from localize key

Usage

Pipes

Use "enumList" pipe to get the array with translation keys:

<div *ngFor="let item of myEnum | enumList: { dictName: 'list' }" [id]="item.id">{{ item.name | i18next }}</div>

Pipe has one required parameter "dictName". It's name of dictionary in localization file. Other params are optional. You can add the folowing parametrs:

nameSpace

<div *ngFor="let item of myEnum | enumList: { dictName: 'list', nameSpace: 'my-favorite-enums' }" [id]="item.id">
 {{ item.name | i18next }}
</div>

You can specify nameSpace parameter for particularly pipes.

ignored

<div *ngFor="let item of myEnum | enumList: { dictName: 'list', ignored: ['Unknown', 'Undefined'] }" [id]="item.id">
 {{ item.name | i18next }}
</div>

Use "enumKey" to get the key of your enum field with all localization path.

 <div>{{ 'keyOfMyEnum' | enumKey: { dictName: 'list', currentEnum: _enums.myEnum, nameSpace: "my-enums"} | i18nextCap}}</div>

Pipe has two required parameters: "dictName" and "currentEnum". "dictName" is the name of dictionary in localization file. "currentEnum" is original enum, where we search the key.

in the end you get result string looks like:

<div>{{ my-enum:list.keyOfMyEnum | i18nextCap }}</div>

nameSpace

<div *ngFor="let item of myEnum | enumList: { dictName: 'list', nameSpace: 'my-favorite-enums' }" [id]="item.id">
 {{ item.name | i18next }}
</div>

Dictionary

Your own dictionary must be looks like:

ru.enums.json

{
"SexKind": {
    "Undefined": "Не выбрано",
    "Male": "Мужской",
    "Female": "Женский"
  }
}

For example you can use this list in native select in html-file:

 <select class="form-control" 
         formControlName="SexKind" 
         [(ngModel)]="model.SexKind">
    <option *ngFor='let status of enums.SexKind | enumList : { dictName: "SexKind" }'
            [ngValue]='status.id'>
        {{ status.name | i18nextCap }}
    </option>
 </select>

"enums" is public variable which contains enums you need to use in your template:

public enums = { SexKind, RaceKind };

angular-enum-list's People

Contributors

aerenfiener avatar efremovdy avatar sir-j avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.