Angular Project

An Angular project = a workspace with angular.json (build, serve and test config) and src/app holding small files that each have one job. Keep shared data in…

Here's what you'll build: one complete order page for Jali's Pancake Stall. Buyers pick from the menu, the cart works out its own count and total, and a form sends the order and then empties itself.

It uses everything you've learned: the menu from @for, a cart in a service with signal and computed, the price pipe, and the order form. Picture the stall's first day open. You've tried the menu board, the order book and the cash box one at a time; today they're all used together, and their numbers have to agree. If the cashier keeps the total on a separate scrap of paper, sooner or later it differs from the order book.

What's in the project

jalis-pancakes/
  angular.json            workspace config: build, serve, test
  package.json
  tsconfig.json
  src/
    index.html            <app-root></app-root>
    main.ts               bootstrapApplication(App, appConfig)
    app/
      app.config.ts       providers for the whole app
      app.ts              the page: menu, cart, form
      cart.store.ts  signal + computed, one source of truth
      menu.ts             the Menu interface and the menu list
      price.pipe.ts       8 becomes $8

The page and its store

// src/app/cart.store.ts
@Injectable({ providedIn: 'root' })
export class CartStore {
  private items = signal<Item[]>([])

  readonly cartItems = this.items.asReadonly()
  readonly count = computed(() => this.items().length)
  readonly total = computed(() => this.items().reduce((t, item) => t + item.price, 0))

  add(item: Item) {
    this.items.update((items) => [...items, item])
  }

  remove(index: number) {
    this.items.update((items) => items.filter((_, i) => i !== index))
  }

  clear() {
    this.items.set([])
  }
}
<!-- the App template (src/app/app.ts) -->
<p class="badge">Items: {{ store.count() }}, {{ store.total() | price }}</p>

@for (m of menu; track m.id) {
  <p>
    {{ m.name }}: {{ m.price | price }}
    <button (click)="store.add(m)" [disabled]="m.stock === 0">Order</button>
  </p>
}

<ul>
  @for (item of store.cartItems(); track $index) {
    <li>{{ item.name }} <button (click)="store.remove($index)">Remove</button></li>
  } @empty {
    <li>The cart is empty</li>
  }
</ul>

<form (ngSubmit)="send()">
  <input name="name" [(ngModel)]="name" placeholder="Name" />
  <button type="submit">Send order</button>
</form>
send() {
  if (!this.name.trim() || this.store.count() === 0) {
    return
  }
  this.orderNumber++
  const total = '$' + this.store.total().toLocaleString('en-US')
  this.lastOrder.set(`Order #${this.orderNumber} for ${this.name}, items: ${this.store.count()}, ${total}`)
  this.store.clear()
  this.name = ''
}

Run in the Playground, step by step:

Action items count() total() What shows
Order Chocolate Cheese Pancake 1 item 1 8 Items: 1, $8
Order Special Egg Pancake 2 items 2 18 Items: 2, $18
Remove the first row 1 item 1 10 Items: 1, $10
Name Sinta, Send order empty 0 0 Order #1 for Sinta, items: 1, $10; The cart is empty; the Name box is empty

Pandan Pancake (stock 0) can't be ordered: [disabled]="m.stock === 0".

One source, changed through update

Say add() is written this.items().push(item) instead of update. Here is the result after two clicks on Order:

update version push version
Cart list 2 rows 2 rows
Badge Items: 2, $18 Items: 0, $0
Send order (name filled in) order recorded nothing happens

The badge stays at 0 items. push changes the old array, so the signal never announces that its contents changed, and computed keeps its old result. The list grows only because the click makes Angular redraw the template and @for reads the same array. send() even stops at its first line, because count() is still 0. Always change a signal through update or set with a new array.

The form pattern: add, then clear

Both project exercises use the same pattern as send():

  1. The list is kept in signal<...>([]), its total in computed(() => this.list().length).
  2. Every input inside <form> uses [(ngModel)] plus name; without name, Angular refuses with NG01352 (the Template-driven Forms lesson).
  3. (ngSubmit) calls a method that ignores empty input, adds the new item with update((list) => [...list, item]), then clears the inputs (this.name = '').
  4. Delete = update((list) => list.filter((x) => x.id !== id)), not hiding the row.

An @empty inside the <ul> draws an <li> when the list is empty. That suits the Contact List, but the Note Taking App checks that ul li is really gone after Delete, so there, don't put @empty inside the <ul>.

ng build: from src to dist

ng build turns every file in src into files ready to upload. It's production by default (see angular.json); ng build --configuration development makes an unminified version for debugging. This app's output:

Initial chunk files   | Names         |  Raw size | Estimated transfer size
main-V7CNGQCQ.js      | main          | 155.05 kB |                43.94 kB
polyfills-RVUGEMAS.js | polyfills     |  34.59 kB |                11.33 kB

                      | Initial total | 189.63 kB |                55.27 kB

Application bundle generation complete. [2.878 seconds]

The result is in the dist/ folder: dist/jalis-pancakes/browser holds index.html, main-V7CNGQCQ.js and polyfills-RVUGEMAS.js. That browser folder is what you upload to hosting. File names change whenever their code changes, so browsers don't use an old version from cache.

⚠️ Numbers that disagree and inputs that don't clear

TL;DR: An Angular project = a workspace with angular.json (build, serve and test config) and src/app holding small files that each have one job. Keep shared data in a service with signal, derive the count and total with computed, and change it through update/set with a new array; push makes the list and the total disagree (2 rows, badge 0 items). The form pattern: [(ngModel)] plus name, (ngSubmit), add with a spread, then clear the inputs. ng build (production by default) writes its output to dist/jalis-pancakes/browser, the folder you upload to hosting.

⚡ The store: one source of truth

Write:

private items = signal<Item[]>([])
readonly count = computed(() => this.items().length)

add(item: Item) {
  this.items.update((items) => [...items, item])
}
Guess first: add() is written this.items().push(item). After Order is clicked twice, what does the badge say?

Items: 0, $0, though the list shows 2 rows. push changes the old array, so the signal never announces anything and computed keeps its old result.

Meaning: Always change a signal through update or set with a new array, and derive the count and the total with computed.

⚡ The form pattern: add, then clear

Write:

<form (ngSubmit)="send()">
  <input name="name" [(ngModel)]="name" placeholder="Name" />
  <button type="submit">Send order</button>
</form>

Meaning: Every input uses [(ngModel)] plus name. (ngSubmit) calls a method that ignores empty input, adds the new item with a spread, then clears the inputs again.

In the Contact List exercise, the grader types Budi into every input and presses Add, then expects Budi in the <ul> and the first input empty again.

⚡ ng build: from src to dist

Write:

ng build

Meaning: ng build already uses the production configuration, because angular.json sets it as the default. Its output goes to dist/jalis-pancakes/browser, the folder you upload to hosting.

What you will learn