[英]Angular2 with Bootstrap Table
I am playing with Angular2 and bootstrap-table ( http://bootstrap-table.wenzhixin.net.cn/ ). 我正在使用Angular2和bootstrap-table( http://bootstrap-table.wenzhixin.net.cn/ )。 In bootstrap table, there is formatter function which is responsible to render cell with custom content. 引导表中有格式化程序功能,该功能负责使用自定义内容呈现单元格。 I would like to put a button in that column but I can't bind click event to that button 我想在该列中放置一个按钮,但无法将click事件绑定到该按钮
import {Component, OnInit, ElementRef, Renderer} from 'angular2/core';
@Component({
selector: 'bs-table',
template: `<table class="table"></table>`
})
export class TableComponent implements OnInit {
constructor(private _elRef: ElementRef, private _renderer: Renderer) {}
edit() {
alert();
}
ngOnInit() {
$(this._elRef.nativeElement.firstChild).bootstrapTable({
columns: [
{title: "ID", field: "id"},
{title: "Name", field: "name"},
{title: "", formatter: (data) => {
return '<button class="btn" (click)="edit()">Edit</button>'; }
},
],
data: [
{id: 1, name: "Test 1"}
]
})
}
}
Angular2 doesn't process HTML added dynamically in any way, therefore (click)
won't be bound in your case. Angular2不会以任何方式处理动态添加的HTML,因此(click)
将不受您的约束。
What you can do is 你能做的是
ngOnInit() {
$(this._elRef.nativeElement.firstChild).bootstrapTable({
columns: [
{title: "ID", field: "id"},
{title: "Name", field: "name"},
{title: "", formatter: (data) => {
return '<button class="btn" (click)="edit()">Edit</button>';
}
},
],
data: [
{id: 1, name: "Test 1"}
]
});
$('button.btn', this._elRef.nativeElement.firstChild).click(...)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.