parent child components in angular code example
Example 1: send event to child component angular
Parent-Component
eventsSubject: Subject<void> = new Subject<void>();
emitEventToChild() {
this.eventsSubject.next();
}
Parent-HTML
<child [events]="eventsSubject.asObservable()"> </child>
Child-Component
private eventsSubscription: Subscription;
@Input() events: Observable<void>;
ngOnInit(){
this.eventsSubscription = this.events.subscribe(() => doSomething());
}
ngOnDestroy() {
this.eventsSubscription.unsubscribe();
}
Example 2: input property angular
<app-slider [title]="'This is a title'"> </app-slider>
.
.
@input() title: string;
constructor(){
}
<div id='slider' class='slider-big'>
<h1> {{title}}</h1>
</div>
Example 3: how to pass variable via component after it's updated angular
<app-child
[data]="data"
(filterEmmiter)="filter($event)">
</app-child>
import {Component, OnChanges, SimpleChanges, Input} from '@angular/core';
class Child implements OnInit, OnChanges {
@Input() Data: any;
constructor() {}
ngOnchanges(changes: SimpleChanges) {
console.log(changes);
}
}