簡體   English   中英

你如何在Angular2組件模板中引用“this”?

[英]How do you reference “this” in an Angular2 component template?

我有一個Angular2組件:

class A {
  filter(..)
}

在A的模板中,我有

<.. list | pipe:filter ..>

管道內部我調用filter,問題是我沒有引用“this”,因為JS是動態范圍的,當我調用它時,我無法訪問過濾器內部的A實例變量。 有沒有辦法獲得“這個”參考?

你不應該需要this 你應該能夠調用filter() ,它應該插入而不需要this 但你需要把它放在{{ }}

{{list | pipe:filter()}}

這是一個吸煙者

如果需要在管道中引用“this”,則可能必須將組件的屬性設置為“this”

export class Component() {
    that = this;
}

然后將“that”作為過濾器中的參數傳遞。

暫無
暫無

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

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