简体   繁体   English

围绕服务实例化的剑道网格奇怪行为

[英]kendo grid odd behaviour around service instantiation

I'm using a Kendo Grid in an Angular template and trying to allow a service in the component to define some css styling but having a problem.我在 Angular 模板中使用 Kendo Grid 并尝试允许组件中的服务定义一些 css 样式但遇到问题。

<kendo-grid
*ngIf="canLoad()"
[data]=etc...
[rowClass] = "rowCallback"
>

public canLoad() : boolean
{
  return this.myservice !== undefined;
}

public rowCallback(context: RowClassArgs)
{
  this.myservice.doSomething();
}

I get an error "Cannot read properties of undefined (reading 'doSomething')" ... why do I get an error when the ngIf check has evaluated myservice as being instantiated?我收到错误消息“无法读取未定义的属性(读取‘doSomething’)” ...为什么当 ngIf 检查已将 myservice 评估为已实例化时我会收到错误消息? How can I get the rowCallback to wait until the service has been instantiated?如何让 rowCallback 等待服务实例化?

In the above example this.myservice.doSomething() this is not pointing to the component class object, so myservice is undefined.在上面的例子this.myservice.doSomething() this没有指向组件 class object,所以 myservice 是未定义的。

To fix this you can either要解决此问题,您可以

  1. Bind this to the method when assigning rowClass分配rowClass时将其绑定this方法
<kendo-grid
*ngIf="canLoad()"
[data]=etc...
[rowClass] = "rowCallback.bind(this)"
>
  1. Use an arrow function to define rowCallback使用箭头 function 定义rowCallback
public rowCallback = (context: RowClassArgs) => {
  this.myservice.doSomething();
}

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

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