DEV Community

Budi Irawan
Budi Irawan

Posted on

3 Different Ways to Use Input Decorator

Input (@Input()) is one of the most used decorators in Angular apps. It is used to pass data from the parent or host component to the child component. This decorator has a relation with DOM property in the template where the child component is used.

There are three different ways to use this decorator.

1) Plain

This is the basic way to use Input decorator.

child-one.component.ts



@Input() message: string;


Enter fullscreen mode Exit fullscreen mode

child-one.component.html



<p>{{ message }}</p>


Enter fullscreen mode Exit fullscreen mode

parent.component.html



<child-one message="hi there"></child-one>


Enter fullscreen mode Exit fullscreen mode

Output:
Alt Text

We have a variable named message and we give it @Input() decorator for that variable. The DOM property in the template parent-component.html will use the same name as the variable name.

2) Alias

By using an alias, the DOM property in the template can use different name. To give alias, we can give an argument in Input decorator such as @Input('my-alias-naming'). Below is the example:

child-two.component.ts



@Input('msg') message: string;


Enter fullscreen mode Exit fullscreen mode

child-two.component.html



<p>{{ message }}</p> <!-- still uses `message` -->


Enter fullscreen mode Exit fullscreen mode

parent.component.html



<child-two msg="hi there"></child-two> <!-- use alias 'msg' -->


Enter fullscreen mode Exit fullscreen mode

Output:
Alt Text

Here, we give an alias msg in Input decorator to message variable. In the child component template (child-component.html), we still refer to it as message.

HOWEVER, in the parent component template (parent-component.html), we refer to it using its alias msg.

3) Setter Getter

Using the same previous examples, consider that we want to display the message in uppercase.

This is probably the common way

child.component.ts



@Input() message: string;

ngOnInit() {
  this.message = this.message && this.message.toUpperCase();
}


Enter fullscreen mode Exit fullscreen mode

or, another approach is using uppercase pipe

child.component.ts



@Input() message: string;


Enter fullscreen mode Exit fullscreen mode

child.component.html



<p>{{ message | uppercase }}</p> <!-- using uppercase pipe -->


Enter fullscreen mode Exit fullscreen mode

Well, there's another way by using set and get from the Typescript class. They are known as auto-properties which are used to access the class variable and we can execute some logic there.

child-three.component.ts



private _message: string;

@Input() // specify Input decorator here
set message(inputMessage: string) {
  this._message = inputMessage && inputMessage.toUpperCase(); // uppercase message here
}

get message(): string {
  return this._message;
}


Enter fullscreen mode Exit fullscreen mode

child-three.component.html



<p>{{ message }}</p>


Enter fullscreen mode Exit fullscreen mode

parent.component.html



<child-three message="hi there"></child-three>


Enter fullscreen mode Exit fullscreen mode

Output

Alt Text

As seen in the code, we define logic to uppercase the message in the set method.

Can we still give an alias using this way? Yes, we can specify alias too such as

child-three.component.ts



@Input('msg') // give alias
set message(inputMessage: string) {
  this._message = inputMessage && inputMessage.toUpperCase(); // uppercase message here
}


Enter fullscreen mode Exit fullscreen mode

parent.component.html



<child-three msg="hi there"></child-three> <!-- use alias 'msg' -->

Enter fullscreen mode Exit fullscreen mode




Demo

Demo on Stackblitz

Summary

Three different ways to use Input decorator. They are plain, alias and set get method. Plain way is the most basic way, the DOM property will have the same name as the variable name. The alias way gives us the flexibility to have different DOM property name. Lastly, set and get allow us to execute some logic.

Top comments (3)

Collapse
 
ahmedgmurtaza profile image
Ahmed Murtaza

Thanks Budi for sharing these tips!

Collapse
 
harshvats2000 profile image
HARSH VATS

Thanks dude! Really helpful and simple.

Collapse
 
dmoncado profile image
Derick Moncado

Great tips man. Thanks!