Latihan 1: Proyek Angular — Angular

Latihan 1 dari materi Proyek Angular di Angular — praktek dengan editor kode interaktif, test case otomatis, dan hint terpandu. Langsung jalan di browser.

Starter-nya sudah punya imports: [FormsModule], interface Contact, name = '', email = '', serta addContact() dan removeContact(id) yang masih kosong. Lengkapi:

  1. Import signal dan computed dari @angular/core. Di class: contacts = signal<Contact[]>([]) dan total = computed(() => this.contacts().length).
  2. Template: <h1>Contact List</h1> dan <p>Total: {{ total() }} kontak</p>.
  3. <form (ngSubmit)="addContact()"> berisi <input [(ngModel)]="name" name="name" placeholder="Nama" />, <input [(ngModel)]="email" name="email" placeholder="Email" />, dan <button type="submit">Tambah</button>.
  4. <ul> dengan @for (contact of contacts(); track contact.id) { <li>{{ contact.name }} - {{ contact.email }} <button (click)="removeContact(contact.id)">Hapus</button></li> } @empty { <li>Belum ada kontak</li> }.
  5. addContact(): berhenti kalau nama atau email kosong, lalu this.contacts.update(list => [...list, { id: Date.now(), name: this.name, email: this.email }]), lalu this.name = '' dan this.email = ''.
  6. removeContact(id): this.contacts.update(list => list.filter(c => c.id !== id)).

Yang dicek:

Hint