diff --git a/README.md b/README.md index 418e444..5ece3c9 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ import { CurrencyMaskModule } from "ng2-currency-mask"; @NgModule({ imports: [ - //... you others modules + ... CurrencyMaskModule ], declarations: [...], @@ -57,4 +57,35 @@ Available options: * `suffix` - Money suffix (default: `''`) * `thousands` - Separator of thousands (default: `','`) +You can also set options globally... + +```ts +import { CurrencyMaskModule } from "ng2-currency-mask"; +import { CurrencyMaskConfig, CURRENCY_MASK_CONFIG } from "ng2-currency-mask/src/currency-mask.config"; + +export const CustomCurrencyMaskConfig: CurrencyMaskConfig = { + align: "right", + allowNegative: true, + allowZero: true, + decimal: ",", + precision: 2, + prefix: "R$ ", + suffix: "", + thousands: "." +}; + +@NgModule({ + imports: [ + ... + CurrencyMaskModule + ], + declarations: [...], + providers: [ + { provide: CURRENCY_MASK_CONFIG, useValue: CustomCurrencyMaskConfig } + ], + bootstrap: [AppComponent] +}) +export class AppModule {} +``` + ## Questions? Open a Issue! \ No newline at end of file diff --git a/index.ts b/index.ts index 7c63489..25f8a1c 100644 --- a/index.ts +++ b/index.ts @@ -1,2 +1,2 @@ -export * from './src/currency-mask.directive'; -export * from './src/currency-mask.module'; \ No newline at end of file +export * from "./src/currency-mask.directive"; +export * from "./src/currency-mask.module"; \ No newline at end of file diff --git a/package.json b/package.json index 5b3ff17..c07fd88 100644 --- a/package.json +++ b/package.json @@ -37,4 +37,4 @@ "typescript": "2.2.1", "zone.js": "0.8.5" } -} +} \ No newline at end of file diff --git a/src/currency-mask.config.ts b/src/currency-mask.config.ts new file mode 100644 index 0000000..be3b4a6 --- /dev/null +++ b/src/currency-mask.config.ts @@ -0,0 +1,15 @@ +import { InjectionToken } from "@angular/core"; + +export interface CurrencyMaskConfig { + + align: string; + allowNegative: boolean; + allowZero: boolean; + decimal: string; + precision: number; + prefix: string; + suffix: string; + thousands: string; +} + +export let CURRENCY_MASK_CONFIG = new InjectionToken("currency.mask.config"); \ No newline at end of file diff --git a/src/currency-mask.directive.ts b/src/currency-mask.directive.ts index 76a031e..0c908dc 100644 --- a/src/currency-mask.directive.ts +++ b/src/currency-mask.directive.ts @@ -1,6 +1,7 @@ -import { AfterViewInit, Directive, DoCheck, ElementRef, forwardRef, HostListener, KeyValueDiffer, KeyValueDiffers, Input, OnInit } from "@angular/core"; +import { AfterViewInit, Directive, DoCheck, ElementRef, forwardRef, HostListener, Inject, KeyValueDiffer, KeyValueDiffers, Input, OnInit, Optional } from "@angular/core"; import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms"; +import { CurrencyMaskConfig, CURRENCY_MASK_CONFIG } from "./currency-mask.config"; import { InputHandler } from "./input.handler"; export const CURRENCYMASKDIRECTIVE_VALUE_ACCESSOR: any = { @@ -31,7 +32,11 @@ export class CurrencyMaskDirective implements AfterViewInit, ControlValueAccesso thousands: "," }; - constructor(private elementRef: ElementRef, private keyValueDiffers: KeyValueDiffers) { + constructor( @Optional() @Inject(CURRENCY_MASK_CONFIG) private currencyMaskConfig: CurrencyMaskConfig, private elementRef: ElementRef, private keyValueDiffers: KeyValueDiffers) { + if (currencyMaskConfig) { + this.optionsTemplate = currencyMaskConfig; + } + this.keyValueDiffer = keyValueDiffers.find({}).create(null); }