Consider this Angular component template snippet:
<button (click)="increment()">Click me</button>
<p>Count: {{ count }}</p>The component class has count = 0; and increment() adds 1 to count. What will be displayed after clicking the button 3 times?
export class CounterComponent { count = 0; increment() { this.count++; } }
Each click triggers the increment() method which increases count by 1.
The (click) event binding calls increment() on each click. Starting from 0, after 3 clicks, count becomes 3 and is displayed.
Given a method selectItem(id: number) in the component, which template syntax correctly passes 42 when the button is clicked?
Event binding calls methods like normal JavaScript functions.
Option B correctly calls selectItem(42) on click. Other options use invalid syntax.
Look at this component snippet:
<button (click)="count = count + 1">Add</button>
<p>Count: {{ count }}</p>And the class:
export class CounterComponent {
count = 0;
}Clicking the button does not update the displayed count. Why?
Angular templates should not modify component properties directly in event bindings.
Angular does not allow direct assignment in templates for change detection. The event binding should call a method that updates count.
message after clicking the button?Given this component template:
<button (click)="updateMessage()">Update</button>
<p>{{ message }}</p>And the class:
export class MsgComponent {
message = 'Hello';
updateMessage() {
setTimeout(() => {
this.message = 'Updated';
}, 0);
}
}What will be displayed immediately after clicking the button?
Consider when setTimeout callback runs relative to Angular change detection.
The message updates asynchronously after the current event cycle, so immediately after click it still shows 'Hello'.
Why does Angular use parentheses (event) syntax for event binding in templates?
Think about how Angular connects user actions to component logic.
Parentheses around event names tell Angular to listen for those DOM events and run the specified code when they happen.