簡體   English   中英

我怎樣才能訪問組件代碼的模板生成的局部變量?

[英]How can I get access to a template generated local variable to component code?

也許我在想完全錯誤的方向。 但是我想做的是在組件中調用模板中定義的變量的值。

<button (click)="download()">...</button>

<ng-container *ngIf="objectlist | filterSort: sortColumnService.sortTerm() as filteredSkaterlist">
    ...
</ng-container>

有什么干凈的方法可以訪問組件的 function 中的filteredSkaterlist嗎? filteredSkaterlist作為參數發送,例如click()不是一個選項,因為按鈕不在 scope 之內。

@Component({...})
export class MyComponent {
    ...
    download() {

        // access value of filteredSkaterlist

    }
}

你能試試這個方法嗎?

<ng-container *ngIf="objectList()">
...
</ng-container>

我已經用 *ngIf 的結構指令定義了 objectList() 方法。 我們可以在組件 class 中定義 objectList() 方法,並根據條件返回 true 或 false。

@Component({...})
export class MyComponent {
   
    objectList() : boolean {

        // access value class objects and return true false value

    }
}

暫無
暫無

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

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