首页 文章

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

提问于
浏览
2

我是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>

在父组件.ts我试图捕获发出的事件 .

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

当我调试代码控件到达子组件中的断点但它没有传递给 cellClicked 方法中的父组件 . 感谢您对此的帮助 .

1 回答

  • 0

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

    在容器父组件中

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

    在父组件中处理事件

    cellClickedInParent(event){
       console.log(event) //////// this works
    }
    

相关问题