使用上下文菜单时的范围界定问题 [英] Scoping issues while using context menu

查看:114
本文介绍了使用上下文菜单时的范围界定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注文档此处将上下文菜单项添加到我的网格中。问题在于,在getContextMenuItems的范围内(在示例中),我无法访问组件中的任何其他方法或变量。这可能吗?下面的示例:

I'm following the docs here to add a context menu item to my grid. The issue is that from the scope of getContextMenuItems (in the example), I'm unable to access any other methods or variables in my component. Is this possible? Example below:

private varIWantToAccess: boolean = false;

function getContextMenuItems(params) {
    var result = [
    { // custom item
        name: 'Alert ' + params.value,
        action: function () 
    {
        window.alert('Alerting about ' + params.value);
        this.varIWantToAccess = true; // Builds fine, but throws a run time exception, since this "this" context is different than the one that has "varIWantToAccess"
    }
    },
        ....
        return result;
}

谢谢!

推荐答案

我假设您正在使用TypeScript谈论Angular 2或4组件。
如果是这样,请使用粗箭头连接到您的函数。

I assume that you are speaking of an Angular 2 or 4 component using TypeScript. If so then use fat arrow to connect to your function.

示例:

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

这应该为您提供所需的范围。

This should provide you the scope you need.

这篇关于使用上下文菜单时的范围界定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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