Hone logo
Hone
Problems

Angular Event Binding Challenge: Interactive Counter

This challenge focuses on implementing event binding in Angular to create a simple interactive counter application. You'll be responsible for updating a counter value displayed on the screen based on user interactions (button clicks). This exercise reinforces fundamental Angular concepts and demonstrates how to connect user actions to component logic.

Problem Description

You need to build an Angular component that displays a counter value and provides two buttons: an "Increment" button and a "Decrement" button. When the "Increment" button is clicked, the counter value should increase by 1. When the "Decrement" button is clicked, the counter value should decrease by 1. The current counter value should be displayed prominently on the component's template.

Key Requirements:

  • Counter Value: A numerical variable within the component should store the counter's current value.
  • Display: The counter value must be displayed in the component's template.
  • Increment Button: Clicking this button should increase the counter value by 1.
  • Decrement Button: Clicking this button should decrease the counter value by 1.
  • Event Binding: Use Angular's event binding syntax to connect the button clicks to the appropriate methods within the component.
  • Two-Way Binding (Optional): While not strictly required, consider how you might use two-way binding to allow the user to directly input a new counter value.

Expected Behavior:

  • The component should initialize with a counter value of 0.
  • Clicking the "Increment" button should increase the counter value and update the displayed value.
  • Clicking the "Decrement" button should decrease the counter value and update the displayed value.
  • The counter value should not go below 0.

Edge Cases to Consider:

  • What happens when the user repeatedly clicks the "Increment" or "Decrement" buttons? The counter should continue to update correctly.
  • How should the component handle the case where the user attempts to decrement the counter below 0? (The counter should remain at 0).

Examples

Example 1:

Initial State: Counter Value = 0
Input: User clicks "Increment" button
Output: Counter Value = 1
Explanation: The increment method is triggered, increasing the counter by 1.

Example 2:

Initial State: Counter Value = 5
Input: User clicks "Decrement" button
Output: Counter Value = 4
Explanation: The decrement method is triggered, decreasing the counter by 1.

Example 3:

Initial State: Counter Value = 0
Input: User repeatedly clicks "Decrement" button
Output: Counter Value = 0
Explanation: The decrement method is triggered repeatedly, but the counter value remains at 0 because it cannot go below 0.

Constraints

  • The solution must be written in TypeScript.
  • The component should be a standard Angular component (using @Component decorator).
  • The solution should be concise and readable.
  • The counter value should be an integer.
  • The component should not rely on external libraries beyond Angular itself.

Notes

  • Consider using the (click) event binding for the buttons.
  • You'll need to define methods within your component to handle the increment and decrement actions.
  • Think about how to update the template to reflect the changes in the counter value. Interpolation ({{ counter }}) is a common approach.
  • You can use a simple HTML structure for the template. Focus on the event binding and component logic.
  • While optional, exploring two-way binding with ngModel can enhance the user experience.
Loading editor...
typescript