Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

typescript - 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!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...