AI Hints
Home›Angular Tutorial›Using Components Together, Component Styles and Practice: A Book Card
Angular · Lesson 27 of 120

Practice: A Book Card

Your turn.

⏱ 2 min read▶ Video at 32:53🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Let's practise components. You'll build a book card, with its own data, and a button.

  • A book card
  • Its own data
  • A button

A book card

Your turn. Pause the video, and try this. Build a book card component, that shows a title, an author, and a price. Add a button, that adds the book to the basket. And show how many copies are in the basket. Here is one solution. The data lives in the class, and the template shows it with curly braces. The click calls add, which adds one. After two clicks, two copies are in the basket.

✍️ Exercise: A book card

Make BookCard: show title, author and price from the class, a button "Add to basket", and "In basket: N" that grows on each click.

Show hint

(click)="add()" and add() { this.inBasket++; }

Show solution
book-card.tstypescript
import { Component } from '@angular/core';

@Component({
  selector: 'app-book-card',
  template: `
    <h3>{{ title }}</h3>
    <p>{{ author }} · £{{ price }}</p>
    <button (click)="add()">Add to basket</button>
    <p>In basket: {{ inBasket }}</p>
  `,
})
export class BookCard {
  title = 'Circe';
  author = 'Madeline Miller';
  price = 10.99;
  inBasket = 0;
  add() { this.inBasket++; }
}

What module three taught

Components have a class, a template, and styles. Curly braces show values. Square brackets set properties. Round brackets listen to events. And the imports array lets components use each other.

keyvalue
{{ }}show values
[ ]set properties
( )listen to events
imports: [ ]use other components

Summary

You can now build, and combine components. That's the end of module three. Next, signals, Angular's way to handle changing data.

Key takeaways
  • Components
  • Bindings
  • Scoped styles
Next up: What Signals Are — values that notify