Angular - (Input) (Output)
Angular - (Input) (Output)
See the live example / download example for a working example containing the code snippets in this
guide.
<<ppaarreenntt--ccoommppoonneenntt>>
<<cchhiilldd--ccoommppoonneenntt>><<∕∕cchhiilldd--ccoommppoonneenntt>>
<<∕∕ppaarreenntt--ccoommppoonneenntt>>
@Input() and @Output() give a child component a way to communicate with its parent component.
@Input() lets a parent component update data in the child component. Conversely, @Output() lets the child
@Input
data flow
Parent Child
In this case, @Input() decorates the property item , which has a type of string , however, @Input()
properties can have any type, such as number , string , boolean , or object . The value for item comes
from the parent component.
src/app/item-detail/item-detail.component.html
<<pp>>
Today's item: {{item}}
<<∕∕pp>>
template is app.component.html .
1. Use the child's selector, here <app-item-detail> , as a directive within the parent component template.
2. Use property binding to bind the item property in the child to the currentItem property of the parent.
src/app/app.component.html
<<aapppp--iitteemm--ddeettaaiill [item]="currentItem">><<∕∕aapppp--iitteemm--ddeettaaiill>>
src/app/app.component.ts
With @Input() , Angular passes the value for currentItem to the child so that item renders as Television .
source
property from
child
parent
selector
<app-item-detail [item]="currentItem"></app-item-detail>
target
@Input()
property from
child
The target in the square brackets, [] , is the property you decorate with @Input() in the child component. The
binding source, the part to the right of the equal sign, is the data that the parent component passes to the nested
component.
@Output
data flow
Parent Child
@Output() marks a property in a child component as a doorway through which data can travel from the child to
the parent.
The child component uses the @Output() property to raise an event to notify the parent of the change. To raise
an event, an @Output() must have the type of EventEmitter , which is a class in @angular∕core that you
use to emit custom events.
The following example shows how to set up an @Output() in a child component that pushes data from an
HTML <input> to an array in the parent component.
event. The EventEmitter then relays the data to the parent component.
1. Import Output and EventEmitter in the child component class:
2. In the component class, decorate a property with @Output() . The following example newItemEvent
src/app/item-output/item-output.component.ts
new Tells Angular to create a new event emitter and that the data it
EventEmitter<string>() emits is of type string.
src/app/item-output/item-output.component.ts
addNewItem(vvaalluuee: ssttrriinngg) {
tthhiiss.newItemEvent.emit(vvaalluuee);
}
}
The addNewItem() function uses the @Output() , newItemEvent , to raise an event with the value the
src/app/item-output/item-output.component.html
The (click) event is bound to the addNewItem() method in the child component class. The addNewItem()
method takes as its argument the value of the #newItem.value property.
src/app/app.component.ts
addItem(newItem: ssttrriinngg) {
tthhiiss.items.push(newItem);
}
}
The addItem() method takes an argument in the form of a string and then adds that string to the items array.
2. Put the child selector, here <app-item-output> , within the parent component's template,
app.component.html .
src/app/app.component.html
<<aapppp--iitteemm--oouuttppuutt (newItemEvent)="addItem($event)">><<∕∕aapppp--iitteemm--oouuttppuutt>>
The $event contains the data that the user types into the <input> in the child template UI.
To see the @Output() working, add the following to the parent's template:
<<uull>>
<<llii *ngFor="let item of items">>{{item}}<<∕∕llii>>
<<∕∕uull>>
The *ngFor iterates over the items in the items array. When you enter a value in the child's <input>
and click the button, the child emits the event and the parent's addItem() method pushes the value to the
items array and new item renders in the list.
src/app/app.component.html
<<aapppp--iinnppuutt--oouuttppuutt
[item]="currentItem"
(deleteRequest)="crossOffItem($event)">>
<<∕∕aapppp--iinnppuutt--oouuttppuutt>>
The target, item , which is an @Input() property in the child component class, receives its value from the
parent's property, currentItem . When you click delete, the child component raises an event, deleteRequest ,
which is the argument for the parent's crossOffItem() method.
The following diagram shows the different parts of the @Input() and @Output() on the <app-input-
output> child component.
source target
property from handler from
child
parent parent
selector
target source
@Input() @Output()
property from event from
child child
The child selector is <app-input-output> with item and deleteRequest being @Input() and @Output()
properties in the child component class. The property currentItem and the method crossOffItem() are both
To combine property and event bindings using the banana-in-a-box syntax, [()] , see Two-way Binding.