简体   繁体   中英

How to call child components's method from the parent component in Angular 6

Parent Component

import { Component } from '@angular/core';
import { ChildComponent }  from './notify.component';

@Component({
    selector: 'my-app',
    template:
    `
    <button (click)="submit()">Call Child Component Method</button>
    `
})
export class AppComponent {
    constructor(private childComp: ChildComponent) { 
    }

    submit(): void {
        // execute child component method
        // This line is compiled properly but at the run time it gives me error related to the static injector## Heading ##
        childComp.callMethod();
    }
}

Child component

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

@Component({
    selector: 'child',
    template: '<h3>Child component {{test}}</h3>'
})
export class ChildComponent implements OnInit {
   test:string; 
   constructor() { }

    ngOnInit() { }

    callMethod(): void {
        console.log('successfully executed.');
        this.test = 'Me';
    }
}

I am getting an error for the static injector, I am not able to inject the child component in the parent component. Here is the error.

StaticInjectorError(AppModule)[AppComponent -> ChildComponent]: StaticInjectorError[ChildComponet]: NullInjectorError: No provider for ChildComponet!

I have added the reference in the Appmodule and added the component in the declaration. Still, I am facing the same issue.

Please Help!!

Update:

just export the child like <app-child #child></app-child> and then you can call all methods using child like : <button> (click)="child.callMethod()">Call</button>

Parent.html

 <app-child #child></app-child> <button (click)="child.callMethod()">Call</button> 

Old answer

You can use @ViewChild by Parent like in example:

Parent

import { Component, ViewChild } from '@angular/core';
import { ChildComponent } from './child.component';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor() { }
  @ViewChild(ChildComponent) private myChild: ChildComponent;
  submit() {
    this.myChild.callMethod();
  }
}

Child:

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

@Component({
  selector: 'child',
  template: `<h3>Child component {{test}}</h3>`
})
export class ChildComponent {
  test = 0;
  callMethod() {
    console.log('successfully executed.');
    this.test++;
  }
}

Here is a worked example

You can achieve this by introducing a concept called @Viewchild which allows allows a one component to be injected into another, giving the parent access to its attributes and functions.

for example:

Parent component

   import { Component, ViewChild, AfterViewInit } from '@angular/core';
   import { ChildComponent } from "../child/child.component";
    @Component({
    selector: 'app-parent',
    template: `
    Message: {{ message }}
 <app-child></app-child>
    `,styleUrls: ['./parent.component.css']
    })
    export class ParentComponent implements AfterViewInit {

@ViewChild(ChildComponent) child;

constructor() { }

message:string;

ngAfterViewInit() {
this.message = this.child.message
  }
 }

child component

    import { Component} from '@angular/core';
    @Component({
    selector: 'app-child',
    template: `
    `,
    styleUrls: ['./child.component.css']
    })
    export class ChildComponent {
    message = 'Hola Mundo!';
    constructor() { }
  }

您需要在app.module.ts的声明下添加ChildComponent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM