项目作者: ZeevKatz

项目描述 :
✨ Decorator that set default values to your component inputs and properties
高级语言: TypeScript
项目地址: git://github.com/ZeevKatz/ngx-input-default-value.git
创建时间: 2019-04-05T07:44:39Z
项目社区:https://github.com/ZeevKatz/ngx-input-default-value

开源协议:

下载


@katze/ngx-input-default-value"">npm
@katze/ngx-input-default-value"">npm
Build Status

Angular | Default value for inputs and properties️ ⚡

A simple way to set default values to your component inputs and properties using a decorator.

🚀 See it in action on Stackblitz


Installation

  1. yarn add @katze/ngx-input-default-value

or

  1. npm install @katze/ngx-input-default-value --save

Usage

  1. import { Component, ChangeDetectionStrategy, OnChanges, SimpleChanges, Input } from '@angular/core';
  2. import { InputDefaultValue } from '@katze/ngx-input-default-value';
  3. type SimpleObject = { bar: string, foo: string };
  4. @Component({
  5. selector: 'simple-component',
  6. templateUrl: './simple-component.component.html',
  7. styleUrls: ['./simple-component.component.scss'],
  8. changeDetection: ChangeDetectionStrategy.OnPush
  9. })
  10. export class SimpleComponent implements OnChanges {
  11. @Input()
  12. @InputDefaultValue<SimpleObject>({
  13. bar: 'Default bar value',
  14. foo: 'Default foo value'
  15. })
  16. simpleObject: Partial<SimpleObject>;
  17. ngOnChanges(changes: SimpleChanges) {
  18. // In case:
  19. // <simple-component></simple-component>
  20. this.simpleObject; // => { bar: 'Default bar value', foo: 'Default foo value' }
  21. // In case:
  22. // <simple-component [simpleObject]="{bar: 'New bar value'}"></simple-component>
  23. this.simpleObject; // => { bar: 'New bar value', foo: 'Default foo value' }
  24. }
  25. }

Use with any class

  1. import { InputDefaultValue } from '@katze/ngx-input-default-value';
  2. type SimpleObject = { bar: string, foo: string };
  3. class SimpleClass {
  4. @InputDefaultValue<SimpleObject>({
  5. bar: 'Default bar value',
  6. foo: 'Default foo value'
  7. })
  8. simpleObject: Partial<SimpleObject>;
  9. }
  10. const instance = new SimpleClass();
  11. instance.simpleObject;
  12. // => { bar: 'Default bar value', foo: 'Default foo value' }
  13. instance.simpleObject = {
  14. bar: 'New bar value'
  15. };
  16. instance.simpleObject;
  17. // => { bar: 'New bar value', foo: 'Default foo value' }
  18. instance.simpleObject = null;
  19. instance.simpleObject;
  20. // => { bar: 'Default bar value', foo: 'Default foo value' }