p2.component.ts 862 Bytes
Newer Older
李彥志's avatar
李彥志 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
import { Component, OnInit } from '@angular/core';
import { Message } from './message';

@Component({
  selector: 'app-p2',
  templateUrl: './p2.component.html',
  styleUrls: ['./p2.component.css']
})
export class P2Component implements OnInit {

    name = '';

    price = '';

    quantity = '';

    messages: Message[] = [];

    result = 0;

    addMessage(): void {

   // 防呆,避免名稱或內容是空值時也可以留言
    if (
      !this.name.trim() && !this.price.trim() && !this.quantity.trim()
    ) {
      return;
    }
    // 用名稱跟內容產生一個留言的資料物件
    const message = new Message(this.name, this.price, this.quantity);
    // 將留言的資料物件放進容器裡
    this.messages.push(message);

  }
    deleteItem(): void {
      alert('清空啦');
    }

  constructor() { }

  ngOnInit() {
  }

}