angular2 ng2-smart-table:从自定义按钮事件

时间:2017-07-27 15:36:48

标签: angular ng2-smart-table

我已经浏览了ng2-smart表,我已经创建了文档中提到的用于实现列单元格功能的列  我已经按照以下链接 https://github.com/akveo/ng2-smart-table/blob/master/src/app/pages/examples/custom-edit-view/basic-example-button-view.component.ts

但我无法捕捉其他组件中ButtonViewComponent中提到的事件。

import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { ViewCell } from 'ng2-smart-table';
import {Routes, RouterModule, Router} from '@angular/router';
import {Observable} from "rxjs";
@Component({
    selector: 'button-view',
    template: `
    <a (click)="onClick()">{{ renderValue }}</a>
  `,
})
export class ButtonViewComponent implements ViewCell, OnInit {
    renderValue: string;

    @Input() value: string | number = '';
    @Input() rowData: any='Hello';

    @Output() save: EventEmitter<any> = new EventEmitter();
    @Output() sendInformationTOANI: EventEmitter<any> = new EventEmitter();

    ngOnInit() {
        this.renderValue = this.value.toString().toUpperCase();
    }
    constructor(private router: Router){

    }
    onClick() {
        this.save.emit(this.rowData);
        console.log('emit data');
// Here i want to catch this event in other component!!
    }


}

@注意:我尝试过使用@Input和@Output,但仍然无法捕获事件 在此先感谢!!

1 个答案:

答案 0 :(得分:1)

假设您已在Parent Component中声明了列,则可以使用 onComponentInitFunction()函数访问子组件的事件。例如

{
  title: "Actions",
  type: "custom",
  renderComponent: ButtonViewComponent,
  onComponentInitFunction :(instance) => {
    instance.save.subscribe(row => {
     //now you can access value from child component here
  });
 }
};