【问题标题】:How to get the component reference on click angular 2?如何在单击角度 2 上获取组件参考?
【发布时间】:2017-10-08 21:23:48
【问题描述】:
<!--snippet from template file-->
<custom-component 
    ... other properties
    (mousedown)="onMouseDown()"
</custom-component>

@Component({
    //selector and templateURL
})
class ContainerComponent{
      onMouseDown(){
          //get the reference to the custom component here
      }
}

所以我的模板文件中有多个自定义组件指向同一个mousedown 事件我如何获得对customComponent 的引用。我想要对组件的真正引用而不是对本机元素的引用,我希望它与 @ViewChild() 事物进行比较。

【问题讨论】:

  • custom-componentContainerComponent是什么关系?
  • custom-componentContainterComponent 的子组件,ContainerComponent 的 html 文件中使用了自定义组件选择器

标签: angular angular2-template angular2-services angular2-directives


【解决方案1】:
import { Component,ViewChild} from '@angular/core';

    <!--snippet from template file-->
    <custom-component #start 
        ... other properties
        (mousedown)="onMouseDown()"
    </custom-component>

    @Component({
        //selector and templateURL
    })
    class ContainerComponent{
          @ViewChild('start') start;
          onMouseDown(){
              //this.start holds the reference
              //get the reference to the custom component here
          }
    }

【讨论】:

  • 这不是问题,页面上有多个自定义组件,都有相同的mousedown事件,怎么知道是哪个点击了呢?
猜你喜欢
  • 2017-12-29
  • 2019-09-11
  • 2017-10-15
  • 1970-01-01
  • 1970-01-01
  • 2019-03-09
  • 2017-04-06
  • 1970-01-01
  • 2018-07-15
相关资源
最近更新 更多