Angular 如何从呈现到ng2智能表中的自定义组件(按钮)捕获事件

Angular 如何从呈现到ng2智能表中的自定义组件(按钮)捕获事件,angular,typescript,Angular,Typescript,我试图在Angular2中发出一个事件,单击ng2智能表中呈现的子组件中的一个按钮,该子组件作为列存在父组件中。由于没有在我渲染的子组件的标记中添加事件侦听器的选项(我需要在标记中执行此操作)。。 这样,只渲染它,而不提供任何属性的选项。 提前谢谢 从自定义组件捕获值 您必须在smartable的设置中声明到父组件中 setting.columns.button: { title: 'Actions', filter: false,

我试图在Angular2中发出一个事件,单击ng2智能表中呈现的子组件中的一个按钮,该子组件作为列存在父组件中。由于没有在我渲染的子组件的标记中添加事件侦听器的选项(我需要在标记中执行此操作)。。 这样,只渲染它,而不提供任何属性的选项。
提前谢谢

从自定义组件捕获值

您必须在smartable的设置中声明到父组件中

   setting.columns.button: {
            title: 'Actions',
            filter: false,
            type: 'custom',
            renderComponent: ActionButtonComponent,
            onComponentInitFunction(instance) {
              instance.save.subscribe(row => {
                console.log(this)
              });
            }
          }
@Component({
  selector: 'app-action-button',
  template: `
 <span ngbDropdown>
    <button class="btn btn-danger btn-raised mr-1" id="dropdownRaised4" ngbDropdownToggle>Actions</button>
    <div ngbDropdownMenu class="dropdown-menu-right" aria-labelledby="dropdownRaised4">
      <button class="dropdown-item ontop" (click)="onClick(event)">
        <i class="icon-x"></i>Cancel</button>
    </div>
  </span>`,
  styleUrls: ['./action-button.component.scss']
})
export class ActionButtonComponent implements ViewCell, OnInit {
  renderValue: string;
  @Input() value: string | number;
  @Input() rowData: any;

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

  ngOnInit() {
    this.renderValue = this.value.toString().toUpperCase();
  }

  onClick(row) {
    this.save.emit(row);
  }


}

和子组件

   setting.columns.button: {
            title: 'Actions',
            filter: false,
            type: 'custom',
            renderComponent: ActionButtonComponent,
            onComponentInitFunction(instance) {
              instance.save.subscribe(row => {
                console.log(this)
              });
            }
          }
@Component({
  selector: 'app-action-button',
  template: `
 <span ngbDropdown>
    <button class="btn btn-danger btn-raised mr-1" id="dropdownRaised4" ngbDropdownToggle>Actions</button>
    <div ngbDropdownMenu class="dropdown-menu-right" aria-labelledby="dropdownRaised4">
      <button class="dropdown-item ontop" (click)="onClick(event)">
        <i class="icon-x"></i>Cancel</button>
    </div>
  </span>`,
  styleUrls: ['./action-button.component.scss']
})
export class ActionButtonComponent implements ViewCell, OnInit {
  renderValue: string;
  @Input() value: string | number;
  @Input() rowData: any;

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

  ngOnInit() {
    this.renderValue = this.value.toString().toUpperCase();
  }

  onClick(row) {
    this.save.emit(row);
  }


}
@组件({
选择器:“应用程序操作按钮”,
模板:`
行动
取消
`,
样式URL:['./操作按钮.component.scss']
})
导出类ActionButtonComponent实现ViewCell,OnInit{
renderValue:字符串;
@Input()值:字符串|数字;
@Input()行数据:任意;
@Output()保存:EventEmitter=neweventemitter();
恩戈尼尼特(){
this.renderValue=this.value.toString().toUpperCase();
}
onClick(行){
this.save.emit(行);
}
}

从自定义组件捕获值

您必须在smartable的设置中声明到父组件中

   setting.columns.button: {
            title: 'Actions',
            filter: false,
            type: 'custom',
            renderComponent: ActionButtonComponent,
            onComponentInitFunction(instance) {
              instance.save.subscribe(row => {
                console.log(this)
              });
            }
          }
@Component({
  selector: 'app-action-button',
  template: `
 <span ngbDropdown>
    <button class="btn btn-danger btn-raised mr-1" id="dropdownRaised4" ngbDropdownToggle>Actions</button>
    <div ngbDropdownMenu class="dropdown-menu-right" aria-labelledby="dropdownRaised4">
      <button class="dropdown-item ontop" (click)="onClick(event)">
        <i class="icon-x"></i>Cancel</button>
    </div>
  </span>`,
  styleUrls: ['./action-button.component.scss']
})
export class ActionButtonComponent implements ViewCell, OnInit {
  renderValue: string;
  @Input() value: string | number;
  @Input() rowData: any;

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

  ngOnInit() {
    this.renderValue = this.value.toString().toUpperCase();
  }

  onClick(row) {
    this.save.emit(row);
  }


}

和子组件

   setting.columns.button: {
            title: 'Actions',
            filter: false,
            type: 'custom',
            renderComponent: ActionButtonComponent,
            onComponentInitFunction(instance) {
              instance.save.subscribe(row => {
                console.log(this)
              });
            }
          }
@Component({
  selector: 'app-action-button',
  template: `
 <span ngbDropdown>
    <button class="btn btn-danger btn-raised mr-1" id="dropdownRaised4" ngbDropdownToggle>Actions</button>
    <div ngbDropdownMenu class="dropdown-menu-right" aria-labelledby="dropdownRaised4">
      <button class="dropdown-item ontop" (click)="onClick(event)">
        <i class="icon-x"></i>Cancel</button>
    </div>
  </span>`,
  styleUrls: ['./action-button.component.scss']
})
export class ActionButtonComponent implements ViewCell, OnInit {
  renderValue: string;
  @Input() value: string | number;
  @Input() rowData: any;

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

  ngOnInit() {
    this.renderValue = this.value.toString().toUpperCase();
  }

  onClick(row) {
    this.save.emit(row);
  }


}
@组件({
选择器:“应用程序操作按钮”,
模板:`
行动
取消
`,
样式URL:['./操作按钮.component.scss']
})
导出类ActionButtonComponent实现ViewCell,OnInit{
renderValue:字符串;
@Input()值:字符串|数字;
@Input()行数据:任意;
@Output()保存:EventEmitter=neweventemitter();
恩戈尼尼特(){
this.renderValue=this.value.toString().toUpperCase();
}
onClick(行){
this.save.emit(行);
}
}

Hi,欢迎来到stack overflow。有关如何提问和相应更新问题的更多详细信息,请参阅链接。您好,欢迎使用stack overflow。有关如何提问和相应更新问题的更多详细信息,请参阅链接。虽然此代码片段可以解决问题,但有助于提高回答质量。请记住,您将在将来回答读者的问题,而这些人可能不知道您的代码建议的原因。嘿,您能告诉我如何使用来自父组件的数据初始化组件吗?虽然此代码片段可以解决问题,但有助于提高您的回答质量。请记住,您将在将来回答读者的问题,这些人可能不知道您代码建议的原因。嘿,您能告诉我如何使用来自父组件的数据初始化组件吗?