如何在单击角度2上获取组件参考?

时间:2017-05-10 09:47:38

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

<!--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()事物进行比较。

1 个答案:

答案 0 :(得分:0)

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
          }
    }