@katze/ngx-input-default-value
TypeScript icon, indicating that this package has built-in type declarations

8.0.7 • Public • Published

npm 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

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

or

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

Usage

import { Component, ChangeDetectionStrategy, OnChanges, SimpleChanges, Input } from '@angular/core';
import { InputDefaultValue } from '@katze/ngx-input-default-value';

type SimpleObject = { bar: string, foo: string };

@Component({
  selector: 'simple-component',
  templateUrl: './simple-component.component.html',
  styleUrls: ['./simple-component.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class SimpleComponent implements OnChanges {
  @Input()
  @InputDefaultValue<SimpleObject>({
    bar: 'Default bar value',
    foo: 'Default foo value'
  })
  simpleObject: Partial<SimpleObject>;
  
  ngOnChanges(changes: SimpleChanges) {
    // In case: 
    // <simple-component></simple-component>
    this.simpleObject; // => { bar: 'Default bar value', foo: 'Default foo value' }
    
    // In case: 
    // <simple-component [simpleObject]="{bar: 'New bar value'}"></simple-component>
    this.simpleObject; // => { bar: 'New bar value', foo: 'Default foo value' }
  }
}

Use with any class

import { InputDefaultValue } from '@katze/ngx-input-default-value';

type SimpleObject = { bar: string, foo: string };

class SimpleClass {
  @InputDefaultValue<SimpleObject>({
    bar: 'Default bar value',
    foo: 'Default foo value'
  })
  simpleObject: Partial<SimpleObject>;
}

const instance = new SimpleClass();

instance.simpleObject;
// => { bar: 'Default bar value', foo: 'Default foo value' }

instance.simpleObject = {
  bar: 'New bar value'
};

instance.simpleObject;
// => { bar: 'New bar value', foo: 'Default foo value' }

instance.simpleObject = null;

instance.simpleObject;
// => { bar: 'Default bar value', foo: 'Default foo value' }

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 8.0.7
    51
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 8.0.7
    51
  • 8.0.6
    1
  • 8.0.5
    1
  • 8.0.4
    1

Package Sidebar

Install

npm i @katze/ngx-input-default-value

Weekly Downloads

41

Version

8.0.7

License

MIT

Unpacked Size

33 kB

Total Files

22

Last publish

Collaborators

  • katze