gpt4 book ai didi

angular - Ag-grid(Angular 2) 无法访问 CellRenderer 中的组件字段

转载 作者:太空狗 更新时间:2023-10-29 17:13:58 28 4
gpt4 key购买 nike

我在尝试对使用 cellRenderer 创建的按钮的单击事件调用服务方法时遇到此问题。

我在 Angular2 中使用 ag-grid。

{ headerName: 'Update', field: "update", width: 80, cellRenderer : this.updateRecord }


updateRecord(params) {
var eDiv = document.createElement('div');
eDiv.innerHTML = '<span class="my-css-class"><button class="edit">Edit</button></span>';
var eButton = eDiv.querySelectorAll('.edit')[0];
eButton.addEventListener('click', function() {
this.myService.getAll().subscribe(data => console.log(JSON.stringify(data)))
});
return eDiv;
}

我收到以下错误:

EXCEPTION: Cannot read property 'getAll' of undefined

此外,我无法访问我的任何组件变量。

最佳答案

cellRenderer 处于不同的上下文中,因此 thisupdateRecord 函数中不可访问。

您需要在gridOptions 中初始化context 参数,并在params 对象中使用它。

以这种方式在您的 gridOptions 对象中设置 context

this.gridOptions.context = {
myService: this.myService
}

您的 myService 将在 context 属性中可用。

updateRecord(params) {
var eDiv = document.createElement('div');
eDiv.innerHTML = '<span class="my-css-class"><button class="edit">Edit</button></span>';
var eButton = eDiv.querySelectorAll('.edit')[0];
eButton.addEventListener('click', function() {
params.context.myService.getAll().subscribe(data => console.log(JSON.stringify(data)))
});
return eDiv;
}

关于angular - Ag-grid(Angular 2) 无法访问 CellRenderer 中的组件字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40395558/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com