skid
skid

Reputation: 978

value undefined in angular2

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

Upvotes: 1

Views: 413

Answers (1)

yurzui
yurzui

Reputation: 214335

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

Upvotes: 3

Related Questions