从父类调用子组件方法 - Angular [英] Call child component method from parent class - Angular

查看:36
本文介绍了从父类调用子组件方法 - Angular的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个子组件,它有一个我想要调用的方法.

当我调用这个方法时,它只会触发 console.log() 行,它不会设置 test 属性??

以下是包含我所做更改的快速入门 Angular 应用.

父母

import { Component } from '@angular/core';import { NotifyComponent } from './notify.component';@成分({选择器:'我的应用',模板:`<button (click)="submit()">调用子组件方法</button>`})导出类 AppComponent {私有通知:NotifyComponent;构造函数(){this.notify = new NotifyComponent();}提交():无效{//执行子组件方法通知.callMethod();}}

儿童

import { Component, OnInit } from '@angular/core';@成分({选择器:'通知',模板:'<h3>通知{{test}}</h3>'})导出类 NotifyComponent 实现 OnInit {测试:字符串;构造函数(){}ngOnInit() { }callMethod(): void {console.log('成功执行');this.test = '我';}}

如何设置 test 属性?

解决方案

您可以使用 @ViewChild 来完成此操作以获取更多信息,请查看此 link

<块引用>

带类型选择器

子组件

@Component({选择器:'child-cmp',模板:'<p>child</p>'})类 ChildCmp {做点什么() {}}

父组件

@Component({选择器:'some-cmp',模板:'<child-cmp></child-cmp>',指令:[ChildCmp]})类 SomeCmp {@ViewChild(ChildCmp) child:ChildCmp;ngAfterViewInit() {//孩子被设置this.child.doSomething();}}

<块引用>

带字符串选择器

子组件

@Component({选择器:'child-cmp',模板:'<p>child</p>'})类 ChildCmp {做点什么() {}}

父组件

@Component({选择器:'some-cmp',模板:'<child-cmp #child></child-cmp>',指令:[ChildCmp]})类 SomeCmp {@ViewChild('child') child:ChildCmp;ngAfterViewInit() {//孩子被设置this.child.doSomething();}}

I have created a child component which has a method I want to invoke.

When I invoke this method it only fires the console.log() line, it will not set the test property??

Below is the quick start Angular app with my changes.

Parent

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

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

    constructor() { 
      this.notify = new NotifyComponent();
    }

    submit(): void {
        // execute child component method
        notify.callMethod();
    }
}

Child

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

@Component({
    selector: 'notify',
    template: '<h3>Notify {{test}}</h3>'
})
export class NotifyComponent implements OnInit {
   test:string; 
   constructor() { }

    ngOnInit() { }

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

How can I set the test property as well?

解决方案

You can do this by using @ViewChild for more info check this link

With type selector

child component

@Component({
  selector: 'child-cmp',
  template: '<p>child</p>'
})
class ChildCmp {
  doSomething() {}
}

parent component

@Component({
  selector: 'some-cmp',
  template: '<child-cmp></child-cmp>',
  directives: [ChildCmp]
})
class SomeCmp {

  @ViewChild(ChildCmp) child:ChildCmp;

  ngAfterViewInit() {
    // child is set
    this.child.doSomething();
  }
}

With string selector

child component

@Component({
  selector: 'child-cmp',
  template: '<p>child</p>'
})
class ChildCmp {
  doSomething() {}
}

parent component

@Component({
  selector: 'some-cmp',
  template: '<child-cmp #child></child-cmp>',
  directives: [ChildCmp]
})
class SomeCmp {

  @ViewChild('child') child:ChildCmp;

  ngAfterViewInit() {
    // child is set
    this.child.doSomething();
  }
}

这篇关于从父类调用子组件方法 - Angular的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆