I am just learning Angular2 and I came across @Input and @Output. What is the difference between the two.
Decorator that marks a class field as an input property and supplies configuration metadata. The input property is bound to a DOM property in the template. During change detection, Angular automatically updates the data property with the DOM property's value.
While Angular inputs/outputs should be used when sharing data to and from child components, ViewChild should be used when trying to utilize properties and methods of the child component directly in the parent component.
The above pattern — passing data in through an “input” property and sending data out through an “output” event — is the primary way to share data between Angular 2 components.
Now, these two decorators have distinct functions: @Ouput - A decorator that lets the child component communicates with the parent component. @Input - A decorator that allows the parent to communicates with the child component.
@Input()
is to pass data In to the component
class ChildComponent {
@Input() data;
}
@Component({
template: `<child [data]="parentData"></child>
})
class ParentComponent {
parentData;
}
Here ParentComponent
is passing data to the child by the @Input()
property. The [data]
is the same name as the property in the child component. If you want to use a different name than the property name, then you can use @Input('diff-name')
@Output
is to emit data (events) Out from a component
class ChildComponent {
@Output() dataChange = new EventEmitter();
click() {
dataChange.emit('new Value');
}
}
@Component({
template: `<child (dataChange)="onDataChange($event)"></child>
})
class ParentComponent {
onDataChange(event) {
console.log(event);
}
}
Here ChildComponent
has an @Output
that it emits events to. The parent is listening and passes a callback to the (dataChange)
. Now every time the child emits an event, the parent callback will be called passing the event.
For AngularJS, we can exchange data by $scope, parent scope and root scope, but not Angular. I think @Input is very easy, so let me focus @Output.
Usually, we can use Service sync data between any components.
import { Injectable } from '@angular/core';
import { Subject, Subscription } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class OneService {
public oneSubject: Subject<Object> = new Subject<Object>();
}
//And we can use it in component
export class ComponentA {
public dataFromService = null;
constructor(private oneService: OneService) {
this.oneService.subscribe(data => this.dataFromService = data);
}
}
//We also can emit changes from other object
export class ComponetB {
public oneData = false;
constructor(private oneService: OneService) {
}
onChange() {
this.oneService.next(this.oneData);
}
}
So, @Output to simplify above logic between parent/children components.
Warning: It is not mean @Output equal subject/subscribe, only make helpful about how to understand output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With