ng2父组件中触发子组件中的方法@ViewsChild()

1、子组件内容

import {Component} from "@angular/core";
@Component({
  selector: "my-contact",templateUrl: "<a (click)='childClick()'>单机事件</a>"
})
export class ChildComponent {
  constructor() {
    console.log("contact");
  }

  childClick() {
    alert("子组件的事件触发");
  }
}

2、父组件

import {Component,ViewChild} from "@angular/core";
import {ChildComponent} from "./child.component";
@Component({
  selector: "my-home",templateUrl: "<a (click)='parentClick()'>单机,调用子组件的方法</a>"
})
export class ParentComponent {
  @ViewChild(ChildComponent) child: ChildComponent;
    parentClick() {
        this.child.childClick();
    }
}

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...