Suhas Parameshwara's answer is nice, but it's missing an unsubscribe to prevent memory leaks which might catch someone out who is copy pasting the solution. There's also a redundant Subject creation (it's also an Observable, not a Subject) which hides an error when you try to subscribe if you forget to pass in an Observable to the child.
Here's a more complete solution:
Parent.Component.html
<child-component [resetForm]="formResetter.asObservable()"></child-component>
<button (click)="resetChildForm()"></button>
Parent.Component.ts
import { Component } from '@angular/core';
import { Subject } from 'rxjs';
@Component({
selector: 'app-parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.css'],
})
export class ParentClass {
public formResetter: Subject<boolean> = new Subject<boolean>();
resetChildForm() {
this.formResetter.next(true);
}
}
Child.Component.ts
import { Component, Input, OnDestroy, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css'],
})
export class ChildClass implements OnInit, OnDestroy {
@Input() resetForm: Observable<boolean>;
private sub: any;
ngOnInit() {
this.sub = this.resetForm.subscribe(response => {
// do whatever operations you need.
});
}
ngOnDestroy(): void {
this.sub.ngUnsubscribe();
}
}