Thuta Learning
Angular
ExercisesWeb DevelopmentIntermediate

Exercise Set 1: Components, Binding & Directives

AngularLesson 4740 min

What you'll walk away with

  • Explain the core concept and Angular data flow of Exercise Set 1: Components, Binding & Directives
  • Run the example, predict its state and UI, and test edge and error cases
  • Apply a typed safe pattern in a real project and recognize common pitfalls

Explain it simply

Exercise Set 1: Components, Binding & Directives, in plain language

This exercise set is designed to give you hands-on practice with Angular's most fundamental building blocks — components, data binding (property/event/two-way), and structural directives (*ngIf, *ngFor, *ngSwitch). Each task can be done standalone, and they're great for refreshing the concepts you learned in the Basic chapter. We recommend attempting each solution yourself before checking the answer as a reference.

Problem → Why → Solution

The problem — why this concept matters

One successful sample render does not cover empty state, repeated items, or accessibility bugs.

A common mistake

One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Core concept — understand how it works

This exercise set tests the ability to combine component boundaries, template binding, events, and directives. Before coding, define the state owner, inputs, outputs, and render branches for each task. Test normal, empty, invalid, and reorder cases, including semantic HTML and keyboard behavior in the acceptance criteria.

Mental model
  1. requirement
  2. reason and implement
  3. test and explain

Explain the core concept and Angular data flow of Exercise Set 1: Components, Binding & Directives

Core syntax or pattern

typescript
// profile-card.component.ts
import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-profile-card',
  template: `
    <div class="card">
      <h3>{{ name }}</h3>
      <p>Age: {{ age }}</p>
    </div>
  `
})
export class ProfileCardComponent {
  @Input() name = '';
  @Input() age = 0;
}

// counter.component.ts (task 2 skeleton)
import { Component } from '@angular/core';

@Component({
  selector: 'app-counter',
  template: `
    <p>Count: {{ count }}</p>
    <button (click)="increment()">+1</button>
  `
})
export class CounterComponent {
  count = 0;
  increment() {
    this.count++;
  }
}

This exercise set tests the ability to combine component boundaries, template binding, events, and directives. Before coding, define the state owner, inputs, outputs, and render branches for each task. Test normal, empty, invalid, and reorder cases, including semantic HTML and keyboard behavior in the acceptance criteria.

Read the code step by step

  1. requirement// profile-card.component.ts

    This exercise set is designed to give you hands-on practice with Angular's most fundamental building blocks — components, data binding (property/event/two-way), and structural directives (*ngIf, *ngFor, *ngSwitch). Each task can be done standalone, and they're great for refreshing the concepts you learned in the Basic chapter. We recommend attempting each solution yourself before checking the answer as a reference.

  2. reason and implement// counter.component.ts (task 2 skeleton)

    This exercise set tests the ability to combine component boundaries, template binding, events, and directives. Before coding, define the state owner, inputs, outputs, and render branches for each task. Test normal, empty, invalid, and reorder cases, including semantic HTML and keyboard behavior in the acceptance criteria.

  3. test and explain}

    Complete each challenge as a component diagram, implementation, behavior tests, and refactor, documenting input and output contracts.

Execution flow
  1. requirement
  2. reason and implement
  3. test and explain

Success

Explain the core concept and Angular data flow of Exercise Set 1: Components, Binding & Directives

Failure

One successful sample render does not cover empty state, repeated items, or accessibility bugs.

Let's think about this for a moment

This exercise set is designed to give you hands-on practice with Angular's most fundamental building blocks — components, data binding (property/event/two-way), and structural directives (*ngIf, *ngFor, *ngSwitch). Each task can be done standalone, and they're great for refreshing the concepts you learned in the Basic chapter. We recommend attempting each solution yourself before checking the answer as a reference.

Exercises

(1) Build a `ProfileCardComponent` and display @Input() name, age via property binding. (2) Use event binding [(click)] so a button click increments a counter number. (3) Two-way bind a text input field with [(ngModel)] and show a real-time preview of the typed text. (4) List an array of 5 strings with *ngFor, and use *ngIf to show a "No items" message when the array is empty.

Code Example

typescript
// profile-card.component.ts
import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-profile-card',
  template: `
    <div class="card">
      <h3>{{ name }}</h3>
      <p>Age: {{ age }}</p>
    </div>
  `
})
export class ProfileCardComponent {
  @Input() name = '';
  @Input() age = 0;
}

// counter.component.ts (task 2 skeleton)
import { Component } from '@angular/core';

@Component({
  selector: 'app-counter',
  template: `
    <p>Count: {{ count }}</p>
    <button (click)="increment()">+1</button>
  `
})
export class CounterComponent {
  count = 0;
  increment() {
    this.count++;
  }
}
You should see
Once all 4 tasks are done, the profile card is displayed, the number goes up every time you click the counter button, the preview text updates instantly as you type into the input field, and the list/empty message displays correctly.

Real-world usage

How to apply it in a real project

Complete each challenge as a component diagram, implementation, behavior tests, and refactor, documenting input and output contracts.

Mini challenge

Build it yourself

Rebuild the Exercise Set 1: Components, Binding & Directives example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
typescript
// profile-card.component.ts
import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-profile-card',
  template: `
    <div class="card">
      <h3>{{ name }}</h3>
      <p>Age: {{ age }}</p>
    </div>
  `
})
export class ProfileCardComponent {
  @Input() name = '';
  @Input() age = 0;
}

// counter.component.ts (task 2 skeleton)
import { Component } from '@angular/core';

@Component({
  selector: 'app-counter',
  template: `
    <p>Count: {{ count }}</p>
    <button (click)="increment()">+1</button>
  `
})
export class CounterComponent {
  count = 0;
  increment() {
    this.count++;
  }
}

Expected: Explain the core concept and Angular data flow of Exercise Set 1: Components, Binding & Directives

Show hint

Complete each challenge as a component diagram, implementation, behavior tests, and refactor, documenting input and output contracts.

Quick quiz 1 / 3

What best describes the core idea of Exercise Set 1: Components, Binding & Directives?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • requirement → reason and implement → test and explain
  • This exercise set tests the ability to combine component boundaries, template binding, events, and directives. Before coding, define the state owner, inputs, outputs, and render branches for each task. Test normal, empty, invalid, and reorder cases, including semantic HTML and keyboard behavior in the acceptance criteria.
  • One successful sample render does not cover empty state, repeated items, or accessibility bugs.
  • Complete each challenge as a component diagram, implementation, behavior tests, and refactor, documenting input and output contracts.

Angular — Overview — Angular

Easy traps

  • One successful sample render does not cover empty state, repeated items, or accessibility bugs.
  • One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Hands-on Exercise

Rebuild the Exercise Set 1: Components, Binding & Directives example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

You'll know it worked when: Once all 4 tasks are done, the profile card is displayed, the number goes up every time you click the counter button, the preview text updates instantly as you type into the input field, and the list/empty message displays correctly.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Exercise Set 1: Components, Binding & Directives | Thuta Learning