How can I access from a child component to another child component in Angular 4 [duplicate]

纵然是瞬间 提交于 2019-12-04 19:17:15

You could archieve this by means of ViewChild and Output

For example:

@Component({
  template: '
     <child-one (clicked)="handleClick()"></child-one>
     <child-two></child-two>
  '
})
export class ParentComponent {
   @ViewChild(ChildOneComponent)
   childOne: ChildOneComponent;

   handleClick(){
    this.childOne.doSomething();
   }
}

In this case:

  • clicked is an Ouput property of ChildOneComponent
  • doSomething is a public method

Another approach only ussing Output and a template variable

@Component({
      template: '
         <child-one (clicked)="childTwo.doSomething()"></child-one>
         <child-two #childTwo></child-two>
      '
    })
    export class ParentComponent {

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!