How to emit an event from parent to child?




    The code creates a Subject<void> and pipes it through the takeUntil() operator to create an Observable. The code then creates a Subscription and pipes the Observable through it. The Subscription then binds itself to the Subject<void>. The code then declares an input property called events. The code then initializes the Subscription and sets the input property to the Observable. The code then calls ngOnInit() and calls ngOnDestroy() when the component is destroyed.

    Library: angular

    import { Observable, Subject } from 'rxjs';
    //Parent component
    eventsSubject: Subject<void> = new Subject<void>();
    emitEventToChild() {"something");
    //Parent template
    <child [events]="eventsSubject.asObservable()"> </child>
    //Child component
    private ngUnsubscribe: Subject<void> = new Subject<void>();
    private eventsSubscription: Subscription;
    @Input() events: Observable<void>;
      this.eventsSubscription = => {
        //data = "something"
    ngOnDestroy() {;
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.