value undefined in angular2

爷,独闯天下 提交于 2019-12-20 05:01:33

问题


I am creating a application in which i am using the Ag-Grid api for listing my database content on the web page. Ag-grid have a predefined api to get the selected row content.

Here i my code :

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);
    }
}

When i console the this.gridOptions inside the onSelectionChanged function i am getting undefined in my console.

Thanks in advance


回答1:


1) Use bind method to retain context this

this.gridOptions.onSelectionChanged = this.onSelectionChanged.bind(this);

2) You can also use Instance Function

private onSelectionChanged = () => {
    console.log(this.gridOptions);
}

3) or inline arrow function

this.gridOptions.onSelectionChanged = () => this.onSelectionChanged();


来源:https://stackoverflow.com/questions/42445201/value-undefined-in-angular2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!