簡體   English   中英

圍繞服務實例化的劍道網格奇怪行為

[英]kendo grid odd behaviour around service instantiation

我在 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();
}

我收到錯誤消息“無法讀取未定義的屬性(讀取‘doSomething’)” ...為什么當 ngIf 檢查已將 myservice 評估為已實例化時我會收到錯誤消息? 如何讓 rowCallback 等待服務實例化?

在上面的例子this.myservice.doSomething() this沒有指向組件 class object,所以 myservice 是未定義的。

要解決此問題,您可以

  1. 分配rowClass時將其綁定this方法
<kendo-grid
*ngIf="canLoad()"
[data]=etc...
[rowClass] = "rowCallback.bind(this)"
>
  1. 使用箭頭 function 定義rowCallback
public rowCallback = (context: RowClassArgs) => {
  this.myservice.doSomething();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM