Angular 5模板无法调用静态方法

时间:2018-02-09 06:50:50

标签: static-methods angular5

在我的组件中,我定义了一个简单的静态方法:

export class AppComponent implements OnInit {
    static onSelectAllPressed(element: AudioElement): void {
    }

然后在我的模板html文件中,我尝试将其链接到按钮点击:

<button mat-button (click)="AppComponent.onSelectAllPressed(element)">Select All</button>

它构建(ng build --prod)没有问题,但是当我点击按钮时我在控制台中收到错误说:

  

错误类型错误:无法读取属性&#39; onSelectAllPressed&#39;未定义的

我无法理解为什么会失败。我们根本不允许访问静态方法吗?

1 个答案:

答案 0 :(得分:0)

这里不需要静态方法,可以使用匿名函数

onSelectAllPressed = (element: AudioElement) => element.whatYouNeed;

很容易调用模板

<button mat-button
    (click)="onSelectAllPressed(element)">
Select All</button>