0
0
Angularframework~20 mins

Event binding with parentheses in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Event Binding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What happens when clicking the button?

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?

Angular
export class CounterComponent {
  count = 0;
  increment() {
    this.count++;
  }
}
ACount: 1
BCount: 0
CCount: 3
DCount: undefined
Attempts:
2 left
💡 Hint

Each click triggers the increment() method which increases count by 1.

📝 Syntax
intermediate
1:30remaining
Which option correctly binds a click event to a method with a parameter?

Given a method selectItem(id: number) in the component, which template syntax correctly passes 42 when the button is clicked?

A<button (click)="selectItem:42">Select</button>
B<button (click)="selectItem(42)">Select</button>
C<button (click)="selectItem='42'">Select</button>
D<button (click)="selectItem{42}">Select</button>
Attempts:
2 left
💡 Hint

Event binding calls methods like normal JavaScript functions.

🔧 Debug
advanced
2:00remaining
Why does this event binding not update the count?

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?

ADirectly modifying <code>count</code> in the template does not trigger Angular change detection.
BThe <code>(click)</code> event binding syntax is invalid and does not run.
CThe <code>count</code> property is private and cannot be changed from the template.
DThe component class lacks a method to update <code>count</code>.
Attempts:
2 left
💡 Hint

Angular templates should not modify component properties directly in event bindings.

state_output
advanced
1:30remaining
What is the value of 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?

AError: Cannot read property 'message'
BUpdated
Cundefined
DHello
Attempts:
2 left
💡 Hint

Consider when setTimeout callback runs relative to Angular change detection.

🧠 Conceptual
expert
2:00remaining
Which option best explains event binding with parentheses in Angular?

Why does Angular use parentheses (event) syntax for event binding in templates?

AParentheses indicate binding to DOM events, allowing Angular to listen and respond to user actions.
BParentheses are used to group multiple events together in a single binding.
CParentheses denote two-way data binding between component and template.
DParentheses are required to call component methods without arguments.
Attempts:
2 left
💡 Hint

Think about how Angular connects user actions to component logic.