将事件从Child传递到Parent Component-Angular

时间:2017-11-03 22:48:35

标签: angular components interaction eventemitter

我是Angular的新手,这是我的问题:

我有一个子组件,我在处理网格的单元格单击事件,在回调中我有一个EventEmitter,并被声明为@Output() cellClicked :any;。 我在回调函数中实例化EventEmitter,如下面的示例代码所示:

        onCellClicked($event){
            this.cellClicked = new EventEmitter();
            this.cellClicked.emit($event);  
        }

在父组件模板中,我放置了这个网格

    <data-grid 
        [gridData]="restrictionsAll.restrictions" 
        [columnDefinitionsPath]="restrictionsAll.agGridConfigPath" 
        (getApi)="getGridApi($event)" 
         >
    </data-grid> 

在父母的component.ts中我试图抓住 发生的事件。

    cellClicked($event)
         {
            console.log('onCellClicked: ' + $event.rowIndex + ' ' + $event.colDef.field);
      }

当我调试时,代码控件到达子组件中的断点但它没有传递给cellClicked方法中的父组件。感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

因为您没有在任何地方实施活动

在容器父组件

<data-grid 
    (cellClicked)="cellClickedInParent($event)"
    [gridData]="restrictionsAll.restrictions" 
    [columnDefinitionsPath]="restrictionsAll.agGridConfigPath" 
    (getApi)="getGridApi($event)" 
     >
</data-grid> 

在父组件中处理事件

cellClickedInParent(event){
   console.log(event) //////// this works
}
相关问题