How to pass data via event from child to parent component in Angular 2

At the child, I have a boolean data want to send to parent if a function method is clicked. What should I do?

Parent code is here. (onChangeDone) to receive an output from the child component and it can trigger a method to receive the event from the child too:

HTML Tag:
...

<childcomp (onChangeDone)="onChangeD($event)"> </childcomp>

....

TS code part:

export class ParentComp {

 onChangeD( result: boolean ) {
        if ( result ) // data changed
          { }
        else { }
    }


...

At the child component, answer is @Output() and together with the EventEmitter:

export class ChildComp implements OnInit {

    @Output() onChangeDone = new EventEmitter<boolean>();
    .....

    clickButton(): void {
     .....
     this.onChangeDone.emit(true);
     ....
    }
Advertisements