angular2中未定义的值
问题描述:
我正在创建一个应用程序,使用Ag-Grid API在网页上列出我的数据库内容。 Ag-grid有一个预定义的api来获取选定的行内容。angular2中未定义的值
在这里,我我的代码:
export class customer_entryComponent{
public gridOptions:GridOptions;
constructor(private wakanda: Wakanda){
this.gridOptions = <GridOptions>{
context : {
componentParent : this
}
};
this.gridOptions.columnDefs = this.createColumnDefs();
this.gridOptions.rowData = this.createRowData();
this.gridOptions.rowSelection = 'single';
this.gridOptions.onSelectionChanged = this.onSelectionChanged;
}
private onSelectionChanged(){
console.log(this.gridOptions);
}
}
当我安慰onSelectionChanged功能我在我的控制台得到未定义内this.gridOptions。
预先感谢
答
1)使用bind
方法保留上下文this
this.gridOptions.onSelectionChanged = this.onSelectionChanged.bind(this);
2)也可以使用实例函数
private onSelectionChanged =() => {
console.log(this.gridOptions);
}
3)或内联箭头功能
this.gridOptions.onSelectionChanged =() => this.onSelectionChanged();
感谢这一个工程 – skid