在angular2中未定义的值 [英] value undefined in angular2

查看:126
本文介绍了在angular2中未定义的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,其中使用Ag-Grid api在网页上列出我的数据库内容。 Ag-grid有一个预定义的api以获取选定的行内容。

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.

在这里,我的代码是:

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时我在控制台中变得不确定。

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

预先感谢

推荐答案

1)使用 bind 方法保留上下文 this

1) Use bind method to retain context this

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

2)您还可以使用实例功能

2) You can also use Instance Function

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

3)或内联箭头功能

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

这篇关于在angular2中未定义的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆