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.
- requirement
- reason and implement
- test and explain
Explain the core concept and Angular data flow of Exercise Set 1: Components, Binding & Directives
Core syntax or pattern
// 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
- requirement
// profile-card.component.tsThis 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.
- 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.
- test and explain
}Complete each challenge as a component diagram, implementation, behavior tests, and refactor, documenting input and output contracts.
- requirement
- reason and implement
- 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
// 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++;
}
}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
// 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
Quick quiz 2 / 3
Quick quiz 3 / 3
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