Latihan 2: Proyek Angular

Latihan 2 dari materi Proyek Angular di Angular: praktik dengan editor kode interaktif, test case otomatis, dan hint terpandu, langsung di browser.

Starter-nya sudah punya imports: [FormsModule], interface Note, template dengan <h1>Notes</h1>, title = '', content = '', serta addNote() dan removeNote(id) yang masih kosong. Lengkapi:

  1. Import signal dan computed. Di class: notes = signal<Note[]>([]) dan total = computed(() => this.notes().length).
  2. Di bawah <h1>Notes</h1>: <p>Total: {{ total() }} catatan</p>.
  3. <form (ngSubmit)="addNote()"> berisi <input [(ngModel)]="title" name="title" placeholder="Judul" />, <textarea [(ngModel)]="content" name="content" placeholder="Konten"></textarea>, dan <button type="submit">Simpan</button>.
  4. <ul> dengan @for (note of notes(); track note.id) { <li><strong>{{ note.title }}</strong> <p>{{ note.content }}</p> <button (click)="removeNote(note.id)">Hapus</button></li> }. Jangan taruh @empty di dalam <ul>: <li>-nya ikut dihitung.
  5. addNote(): berhenti kalau judulnya kosong, lalu this.notes.update(list => [...list, { id: Date.now(), title: this.title, content: this.content }]), lalu kosongkan title dan content.
  6. removeNote(id): this.notes.update(list => list.filter(n => n.id !== id)).

Yang dicek:

Hint