简体   繁体   English

如何在Angular 6中从父组件调用子组件的方法

[英]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! StaticInjectorError(AppModule)[AppComponent-> ChildComponent]:StaticInjectorError [ChildComponet]:NullInjectorError:没有为ChildComponet提供程序!

I have added the reference in the Appmodule and added the component in the declaration. 我在Appmodule中添加了引用,并在声明中添加了组件。 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> 只需像<app-child #child></app-child>那样导出<app-child #child></app-child> ,然后您就可以使用child来调用所有方法,例如: <button> (click)="child.callMethod()">Call</button>

Parent.html 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的@ViewChild

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. 您可以通过引入一个称为@Viewchild的概念来实现此目的,该概念允许将一个组件注入另一个组件,从而使父级可以访问其属性和功能。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Angular2父组件中的组件列表中调用子组件中的方法? - How to call a method in child component from a list of components in parent component in Angular2? 如何从父组件调用子组件方法 - How to call child components method from parent component Angular 7+,从父级调用子级组件的方法好吗? - Angular 7+, Is it good to call child component's method from parent? Angular 4 - 如何在没有EventEmitter的情况下从子组件调用父方法? - Angular 4 - How to call parent method from child component without EventEmitter? Angular4-从父组件到子模式组件的方法调用 - Angular4 - Call a method from parent component to child modal component 从父组件角度调用子组件方法 - Angular call child component method from parent component 我的子组件如何在Angular 2中的父组件上调用方法? - How can my child component call a method on the parent component in Angular 2? Angular2从子组件中调用一个伟大的祖父母的方法 - Angular2 call a great grand parent's method from a child component Angular中如何将父组件的数据共享给多个子组件? - How to share the data from Parent Component to Multiple Child components in Angular? 如何在promise中从角度4的子组件中调用父组件中的方法? - How to call a method in parent component from child component in angular 4 within a promise?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM