app.component.ts 759 Bytes
Newer Older
李彥志's avatar
李彥志 committed
1
import { Component } from '@angular/core';
李彥志's avatar
李彥志 committed
2
import { Message } from './message';
李彥志's avatar
李彥志 committed
3 4 5 6 7 8 9

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
李彥志's avatar
李彥志 committed
10 11 12 13 14 15 16 17 18

  name = '';

  price = '';

  quantity = '';

  messages: Message[] = [];

李彥志's avatar
李彥志 committed
19 20
  result = 0;

李彥志's avatar
李彥志 committed
21 22 23 24
  addMessage(): void {

 // 防呆,避免名稱或內容是空值時也可以留言
  if (
李彥志's avatar
李彥志 committed
25
    !this.name.trim() && !this.price.trim() && !this.quantity.trim()
李彥志's avatar
李彥志 committed
26 27 28 29 30 31 32 33 34
  ) {
    return;
  }
  // 用名稱跟內容產生一個留言的資料物件
  const message = new Message(this.name, this.price, this.quantity);
  // 將留言的資料物件放進容器裡
  this.messages.push(message);

}
李彥志's avatar
李彥志 committed
35 36
  deleteItem(): void {
    alert('清空啦');
李彥志's avatar
李彥志 committed
37
  }
李彥志's avatar
李彥志 committed
38
}