简体   繁体   English

在ag-grid中为Angular 2使用gridOptions中的变量

[英]Use variables in gridOptions in ag-grid for Angular 2

I'm working on a table in my Angular 2 application. 我正在Angular 2应用程序中的表上工作。 I'm using ag-grid. 我正在使用ag-grid。 this is my gridOptions 这是我的gridOptions

        this.gridOptions = <GridOptions>{

        onGridReady: (() => {
            this.gridOptions.api.sizeColumnsToFit();
            this.gridOptions.onFilterModified = function () {
                console.log("Value Changed")
            }
        })
    }

This code works fine. 此代码可以正常工作。 If i change the value in the search field, i will get the text "Value Changed" in the console. 如果我在搜索字段中更改了值,我将在控制台中看到文本“ Value Changed”。 I want to use a variable in the OnFilterModified function that is defined outside that function , but it doesn't work. 我想在该函数之外定义的OnFilterModified函数中使用一个变量,但是它不起作用。

console.log(this.) after the dot i only get onGridReady function. 点后的console.log(this。)我只能得到onGridReady函数。 That's it. 而已。 How can i access other functions/variables in the OnFilterModified function? 如何在OnFilterModified函数中访问其他函数/变量?

This isn't really an ag-grid or angular 2 quesion, but rather a javascript scoping one. 这实际上不是ag-grid或angular 2问题,而是JavaScript范围界定的问题。

In the function (and I assume you're just using javascript here, not typescript) "this" isnt the out scope, but rather the functions, which isn't useful. 在函数中(我假设您只是在这里使用javascript,而不是打字稿),“ this”不是超出范围,而是函数,没有用。

The easiest fix for you is to do the following: 最简单的解决方法是执行以下操作:

    var that = this;
    this.gridOptions = <GridOptions>{
        onGridReady: (() => {
            that.gridOptions.api.sizeColumnsToFit();
            that.gridOptions.onFilterModified = function () {
                console.log(that.gridOptions)
                console.log("Value Changed")
            }
        })
    };

I'd check out this (there are many articles on this subject) for more information about JavaScript scope: http://ryanmorr.com/understanding-scope-and-context-in-javascript/ for a better understanding of the issue. 我将检查该主题(关于这个主题的文章很多),以获取有关JavaScript范围的更多信息: http : //ryanmorr.com/understanding-scope-and-context-in-javascript/以更好地理解这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM