Exercise: Shopping List in Angular

Build the shopping list again, in Angular. Hold the items in a signal, work out the count with computed, bind the box with ngModel, draw the list with @for and @if, and let ten tests check it the way a person would use it.

Apprentice · Angular · Updated

You built a shopping list in plain JavaScript in Exercise: Shopping List and Calculator. Now build it in Angular, and notice what you don't write: no getElementById, no createElement, no updating the count by hand.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-shopping-list-in-angular, also linked from the Exercise box beside the lesson. Clone it into your bootcamp folder. In Git Bash:

cd ~/bootcamp
git clone https://github.com/ask-codey/exercise-shopping-list-in-angular.git

Git makes a folder, exercise-shopping-list-in-angular, with the exercise code inside.

Open the folder in VS Code, and a terminal there:

npm install
npm test

npm install downloads the packages, once. npm test runs the tests with Vitest: they all fail to start with.

File What's in it
src/app/app.ts The App component, which shows the shopping list
src/app/shopping-list/shopping-list.ts The component's class, for you to finish. Its requirements are in the comment at the top.
src/app/shopping-list/shopping-list.html Its template, for you to finish
src/app/shopping-list/shopping-list.css Its styles, finished
src/app/shopping-list/shopping-list.spec.ts Ten tests that type, click and read the page

What to do

Make the list do everything the requirements say:

  • a box (#new-item) and an Add button (#add); Enter adds too; blank items and items already on the list aren't added;
  • the items as <li>s, each with its name in a <span> and a Remove button; Your list is empty when there are none;
  • clicking a name crosses it off (the class done), and clicking again puts it back;
  • #items-left: Nothing left to buy, 1 item left, 3 items left;
  • a Clear crossed-off items button (#clear-done), there only while something is crossed off.

Run npm test as you go. When they all pass, run the app with ng serve and try it at http://localhost:4200.

Hints

  • The state: two signals, the items (signal<ShoppingItem[]>([])) and the text in the box (signal('')).
  • Worked out from the state: the number left and its message are computeds, and so is "is anything crossed off?" for the @if.
  • The box: [(ngModel)]="newItem", with FormsModule in the component's imports, plus (keyup.enter)="add()".
  • The list: @for (item of items(); track item.name) { ... } @empty { ... }, and [class.done]="item.done" on each <li>.
  • Changing one item: make a new list, with a new copy of that item: items.update(items => items.map(i => i === item ? { ...i, done: !i.done } : i)). { ...i, done: ... } copies all of i's properties, then changes done, like the array spread [...items] you've used.
  • Removing: filter keeps everything except the item.
  • Read a failing test's message: it says what it expected and what it found, and the tests read the page exactly as described in the requirements.

When you're done

All ten tests pass, and the app works in the browser: add milk, bread and coffee, cross off two, clear them, and bread is left, with 1 item left below it.

Compare it with your JavaScript version: the template says what the page shows, and Angular does all the finding, creating and updating that you did by hand before.

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.

  1. On GitHub, click +, then New repository. Name it (for example exercise-shopping-list-in-angular), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. In a terminal in your exercise folder (Git Bash on Windows), send your work to your new repo. Point your copy at your own repo, then commit and push:

    git remote set-url origin https://github.com/your-name/exercise-shopping-list-in-angular.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-shopping-list-in-angular, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

So what

Signals for the state, computed for what follows from it, bindings to show it, events to change it. That's an Angular component, and every component you build from here works the same way.

Key terms

Related