Performance: Event binding with parentheses
This affects how quickly user interactions trigger JavaScript code and how efficiently the browser updates the UI in response.
<button (click)="doSomething()">Click me</button><button onclick="doSomething()">Click me</button>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Inline HTML event handlers | No extra DOM nodes, but inline attributes parsed | Minimal reflows | Low paint cost | [!] OK |
| Angular (click) event binding per element | Adds native event listeners per element | Minimal reflows | Low paint cost | [OK] Good |
| Angular event binding with delegation | Single event listener on parent | Minimal reflows | Low paint cost | [OK] Good |
(click)="onClick()"handleClick in Angular?(click)="handleClick()". Square brackets are for property binding, not events.<button (click)="increment()">Click me</button>
<p>Count: {{count}}</p>
count = 0;
increment() { this.count += 1; }
<button click="doSomething()">Press</button>
click="doSomething()" without parentheses, so Angular won't bind the event.submitForm() only when the user presses the Enter key inside an input box. Which Angular event binding syntax correctly achieves this?<input (??? )="submitForm()" />
(keyup.enter) to listen only for Enter key on keyup.(keyup.enter) triggers submitForm() only when Enter is released, which is the recommended approach.Pass the test and this pattern moves unknown