跳到正文
本页尚未翻译成中文,以下为英文原文。 查看英文版

Functions should be invoked in event bindings. ​

This diagnostic detects uninvoked functions in event bindings.

ts
import {Component, signal, Signal} from '@angular/core';

@Component({
  template: `<button (click)="onClick">Click me</button>`,
})
class MyComponent {
  onClick() {
    console.log('clicked');
  }
}

What's wrong with that? ​

Functions in event bindings should be invoked when the event is triggered. If the function is not invoked, it will not execute when the event is triggered.

What should I do instead? ​

Ensure to invoke the function when you use it in an event binding to execute the function when the event is triggered.

ts
import {Component} from '@angular/core';

@Component({
  template: `<button (click)="onClick()">Click me</button>`,
})
class MyComponent {
  onClick() {
    console.log('clicked');
  }
}

Configuration requirements ​

strictTemplates must be enabled for any extended diagnostic to emit. uninvokedFunctionInEventBinding has no additional requirements beyond strictTemplates.

What if I can't avoid this? ​

This diagnostic can be disabled by editing the project's tsconfig.json file:

json
{
  "angularCompilerOptions": {
    "extendedDiagnostics": {
      "checks": {
        "uninvokedFunctionInEventBinding": "suppress"
      }
    }
  }
}

See extended diagnostic configuration for more info.

基于 MIT 许可证发布。