88

I want to call parent method (deletePhone) in child component in Angular 4. How can I do that properly?

my parent component looks like:

export class ContactInfo implements OnInit {
    phoneForm: FormGroup;
    phones: Phone[];


    constructor(private fb: FormBuilder,
            private userService: UserService) {
    }

    ngOnInit() {
        this.userService.getDataPhones().subscribe(
            phones => {
                this.phones = phones;
            });

        this.phoneForm = this.fb.group({
            phone: ['', [Validators.pattern(PHONE_PATTERN)]]
        });
    }

    deletePhone(phone: Phone) {
        this.userService.deleteUserPhone(phone)
            .subscribe(res => {
                let index = this.phones.indexOf(phone);
                if (index > -1) {
                    this.phones.splice(index, 1);
                }
        });
    }
}
conceptdeluxe
  • 3,753
  • 3
  • 25
  • 29
Ihor Lavs
  • 2,315
  • 2
  • 15
  • 26

4 Answers4

172
import { Output, EventEmitter } from '@angular/core'; 

...

class ChildComponent {
  @Output() someEvent = new EventEmitter<string>();

  callParent(): void {
    this.someEvent.next('somePhone');
  }
}

In ContactInfo's template

<child-component (someEvent)="deletePhone($event)"
andy
  • 129
  • 3
  • 12
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
23

This worked for me (example from official docs):

https://angular.io/api/core/EventEmitter#examples

Child:

@Component({
  selector: 'zippy',
  template: `
  <div class="zippy">
    <div (click)="toggle()">Toggle</div>
    <div [hidden]="!visible">
      <ng-content></ng-content>
    </div>
 </div>`})
export class Zippy {
  visible: boolean = true;
  @Output() open: EventEmitter<any> = new EventEmitter();
  @Output() close: EventEmitter<any> = new EventEmitter();

  toggle() {
    this.visible = !this.visible;
    if (this.visible) {
      this.open.emit(null); //emit event here
    } else {
      this.close.emit(null);
    }
  }
}

Parent:

<zippy (open)="onOpen($event)" (close)="onClose($event)"></zippy>
conceptdeluxe
  • 3,753
  • 3
  • 25
  • 29
Nitin Jadhav
  • 6,495
  • 1
  • 46
  • 48
15

I don't like boilerplate code like @Output(). I found another solution, just pass object with any number of anonymous functions

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-parent',
  styleUrls: ['./parent.component.css'],
  template: `
  <app-child [parentApi]="getParentApi()"></app-child>
`,
})
export class ParentComponent implements OnInit {

  getParentApi(): ParentComponentApi {
    return {
      callParentMethod: (name) => {
        this.parentMethod(name)
      }
    }
  }

  constructor() { }

  ngOnInit() {
  }

  parentMethod(name: string) {
    console.log(`Hello ${name} from parent`)
  }
  
}

export interface ParentComponentApi {
  callParentMethod: (string) => void
}

And child:

import { Component, OnInit, Input } from '@angular/core';
import { ParentComponentApi } from '../parent/parent.component';

@Component({
  selector: 'app-child',
  template: `<button (click)="callParent()">call parent</button>`,
  styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {

  @Input() parentApi: ParentComponentApi

  constructor() { }

  callParent() {
    this.parentApi.callParentMethod("child")
  }

  ngOnInit() {

  }

}

I think this is pretty safe to do this way, no?

Rwanou
  • 431
  • 5
  • 24
Alexander Kondaurov
  • 3,677
  • 5
  • 42
  • 64
  • 4
    I like the idea of this. Unfortunately doesn't work in Angular 7. Gives a template parse error. – Jans Rautenbach Nov 02 '18 at 21:53
  • 1
    I prefer this solution, working fine for me in Angular 10 – Ron Oct 29 '20 at 14:57
  • `@Output` is not boilerplate, if you are considering `@Input`. They both have different aspects in parent-child relationship. we can not replace them with one another. – Ram Dec 01 '20 at 03:16
  • 1
    i also do prefer this solution, it is easier to understand and implement especially when you function has multiple params. for the `@Output` way of doing you have to drastically change the way your method is implemented to do the same job – Big Zed Oct 28 '21 at 12:14
4

It's simpler than you might think. The key is to pass a parent method to a child @Input property. test it online

Parent component

@Component({
  selector: 'my-app',
  template: `
    <h1>Parent:</h1>
    <p>Parent counting: {{this.count}}</p>

    <child-comp [childInc]="this.inc"></child-comp>
  `
})
export class AppComponent {
  name = "I'm Parent";

  count = 0;

  inc = () => {
    this.count++;
  }
}

Child component

@Component({
  selector: 'child-comp',
  template: `
  <h1>Child:</h1> 
  <button (click)="childInc()">Click me!</button>
  `
})
export class ChildComponent  {
  name = "I'm child"

  @Input() childInc: () => void
}

I've used inc = () => {...} notion in parent, which can remember the right this. If you use the inc(){...} notion, then you need to bind parent this as [childInc]="this.inc.bind(this)".

Iceberg
  • 2,744
  • 19
  • 19
  • how can send parameter this way to parent component? –  Aug 09 '22 at 11:42
  • @amirkian Try to introduce a new function in `ChildComponent` like `(click)="newFunc()"`, and in this newFunc() call the childInc() with parameter. – Iceberg Aug 09 '22 at 14:52