首页 > 其他 > 详细

angular 父组件调用子组件

时间:2018-04-25 11:23:30      阅读:200      评论:0      收藏:0      [点我收藏+]
import { Component, OnInit, ViewChild } from @angular/core;

@Component({
  selector: app-child,
  templateUrl: ./child.component.html,
  styleUrls: [./child.component.css]
})
export class ChildComponent implements OnInit {



  constructor() { }

  ngOnInit() {
  }
  greeting(str: string) {
    console.log(str);
  }

}
<p>
  child works!
</p>
<app-child #child1></app-child>
<app-child #child2></app-child>
<button (click)="child2.greeting(‘child2的问候‘)">点我</button>
import { Component, ViewChild } from @angular/core;
import { ChildComponent } from ./child/child.component;

@Component({
  selector: app-root,
  templateUrl: ./app.component.html,
  styleUrls: [./app.component.css]
})
export class AppComponent {

  @ViewChild(child1)
  child1: ChildComponent;

  constructor() {

  }
  // tslint:disable-next-line:use-life-cycle-interface
  ngOnInit(): void {
    this.child1.greeting(child1的问候);
  }
}

 

angular 父组件调用子组件

原文:https://www.cnblogs.com/chenyishi/p/8941123.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!