網站搜尋

angular - 如何發出一個帶有其值的物件


我有一個 EventEmitter,我正在發出如下的物件:

@Output() cellClick = new EventEmitter();
private _cellClicked(data: any){
      let emitData: any = {
        colId: data.column.colId,
        rowId: data.node.id,
        item: {}
      };
      if (emitWithdata){
          this.cellClick.next(emitData); // Here  i need to send my data also, and it need to append to the value Data.item=data not just the Data object.
      } 
      if (emitWithNodata){
              this.cellClick.next(emitData); // here i just pass the emitData with empty item, which is correct
      }


}

如何使用 emitData 傳遞資料?有什麼想法嗎,各位?提前感謝

答案#1

如果我們遵循 Angular EventEmitter API:https://angular.io/docs/ts/latest/api/core/index/EventEmitter-class.html

  1. 將 EventEmiter 宣告改為使用 : EventEmitter
  2. 要傳送資料,你必須使用 emmit 方法。

你的程式碼應該像這樣:

@Output() cellClick: EventEmitter<any> = new EventEmitter();
private _cellClicked(data: any){
      let emitData: any = {
        colId: data.column.colId,
        rowId: data.node.id,
        item:{}
      };
      if (emitWithdata){
          emitData.item=data;
          this.cellClick.emit(emitData); // Here  i need to send my data also, and it need to append to the value Data.item=data not just the Data object.
      } 
}

答案#2

要將資料傳回事件處理程序,你可以這樣寫:

this.cellClick.emit({data : emitData, orderData: someData});