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.
<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? How can I get the rowCallback to wait until the service has been instantiated?
CodePudding user response:
In the above example this.myservice.doSomething()
this
is not pointing to the component class object, so myservice is undefined.
To fix this you can either
- Bind
this
to the method when assigningrowClass
<kendo-grid
*ngIf="canLoad()"
[data]=etc...
[rowClass] = "rowCallback.bind(this)"
>
- Use an arrow function to define
rowCallback
public rowCallback = (context: RowClassArgs) => {
this.myservice.doSomething();
}