diff --git a/src/app/app.component.html b/src/app/app.component.html index 50cc441..beecf5f 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1 +1,14 @@ -Loading 圖示位置:{{ loadingPath }} + + + + + + + + + + + + + +
單價數量
{{ item.UnitPrice }}{{ item.PurchaseCount }}
總金額{{ total }}
diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 432045b..16758be 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,10 +1,24 @@ -import { Component, Inject } from '@angular/core'; +import { Component, Inject, OnInit } from '@angular/core'; + +import { OrderDetail } from './order-detail'; +import { IOrderService, ORDER_SERVICE } from './order.interface'; @Component({ selector: 'my-app', templateUrl: './app.component.html', - styleUrls: [ './app.component.css' ] + styleUrls: ['./app.component.css'], }) -export class AppComponent { - constructor(@Inject('LoadingPath') public loadingPath: string) {} +export class AppComponent implements OnInit { + total = 0; + + //@Inject 注入Service(ORDER_SERVICE) Token + constructor(@Inject(ORDER_SERVICE) public orderService: IOrderService) {} + + ngOnInit(): void { + this.orderService.details = [ + new OrderDetail({ PurchaseCount: 2, UnitPrice: 200 }), + new OrderDetail({ PurchaseCount: 4, UnitPrice: 50 }), + ]; + this.total = this.orderService.computeTotal(); + } } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index a1fcff5..2b7d538 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -3,11 +3,13 @@ import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { AppComponent } from './app.component'; +import { ORDER_SERVICE } from './order.interface'; +import { OrderService } from './order.service'; @NgModule({ - imports: [ BrowserModule, FormsModule ], - declarations: [ AppComponent ], - providers: [{ provide: 'LoadingPath', useValue: 'assets/loading.gif'}], - bootstrap: [ AppComponent ] + imports: [BrowserModule, FormsModule], + declarations: [AppComponent], + providers: [{ provide: ORDER_SERVICE, useClass: OrderService }], + bootstrap: [AppComponent], }) -export class AppModule { } +export class AppModule {} diff --git a/src/app/order-detail.ts b/src/app/order-detail.ts new file mode 100644 index 0000000..885abab --- /dev/null +++ b/src/app/order-detail.ts @@ -0,0 +1,9 @@ +export class OrderDetail { + constructor(initData?: Partial) { + Object.assign(this, initData); + } + + PurchaseCount!: number; + + UnitPrice!: number; +} diff --git a/src/app/order.interface.ts b/src/app/order.interface.ts new file mode 100644 index 0000000..2d38ae0 --- /dev/null +++ b/src/app/order.interface.ts @@ -0,0 +1,8 @@ +import { InjectionToken } from "@angular/core"; +import { OrderDetail } from "./order-detail"; +export const ORDER_SERVICE = new InjectionToken('Order Service'); + +export interface IOrderService { + details: OrderDetail[]; + computeTotal():number; +} diff --git a/src/app/order.service.ts b/src/app/order.service.ts new file mode 100644 index 0000000..24fa7d4 --- /dev/null +++ b/src/app/order.service.ts @@ -0,0 +1,19 @@ +import { Injectable } from '@angular/core'; + +import { OrderDetail } from './order-detail'; +import { IOrderService } from './order.interface'; + +@Injectable({ + providedIn: 'root', +}) +export class OrderService implements IOrderService { + details: OrderDetail[] = []; + + constructor() {} + + computeTotal(): number { + return this.details + .map((d) => d.PurchaseCount * d.UnitPrice) + .reduce((act, curr) => act + curr, 0); + } +}