From 5b4edd004b3a96910c6cca2c9c8a60df4ea49661 Mon Sep 17 00:00:00 2001
From: Kun-Neng @Output()
,您要在一個元件中引發一個事件,如此一來,其他的元件就會知道有可使用的資料。
In the app
directory, create a new file named item.ts
with the following contents:
在 app
目錄中新建一個名為 item.ts
的檔案,包含以下內容:
export interface Item { description: string; done: boolean; }-
The Item
interface
creates an item
object model so that your application understands what an item
is.
-For this to-do list, an item
is an object that has a description and can be done.
這個 Item
interface
用來建立一個 item
物件模型,讓您的應用程式能理解 item
到底是什麼。
+對於這個待辦列表,一個 item
就代表一個擁有說明與是否已完成等兩個屬性的物件。
Now that your application knows what an item
is, you can give it some items by adding them to the TypeScript file, app.component.ts
.
-In app.component.ts
, replace the contents with the following:
現在,您的應用程式知道了 item
為何物,您可以在 TypeScript 檔案 app.component.ts
中加入更多項目。
+在 app.component.ts
中置換內容如下:
import { Component } from '@angular/core'; @@ -122,29 +122,28 @@ export class AppComponent { }-
The first line is a JavaScript import that imports Angular.
-The @Component()
decorator specifies metadata about the AppComponent
.
-The default metadata properties are as follows:
第一行是以 JavaScript import 來匯入 Angular。
+這個 @Component()
裝飾子指定關於 AppComponent
的元資料(metadata)。
+預設元資料的屬性如下:
selector
: Tells you the name of the CSS selector that you use in a template to instantiate this component. Here it is 'app-root'
.
-In the index.html
, within the body
tag, the Angular CLI added <app-root></app-root>
when generating your application.
-You use all component selectors in the same way by adding them to other component HTML templates.templateUrl
: Specifies the HTML file to associate with this component.
-Here it is, './app.component.html',styleUrls
: Provides the location and name of the file for your styles that apply specifically to this component. Here it is './app.component.css'
.selector
: 代表您使用在範本內的 CSS 選擇器名稱,用以實例化這個元件。此處為 'app-root'
。
+當產生您的應用程式時,於 index.html
檔案中的 body
標籤內, Angular CLI 就已加入了 <app-root></app-root>
。
+您可以使用相同的方式來將元件選擇器加入至其他元件的 HTML 範本內。templateUrl
: 指定與這個元件相關的 HTML 檔案。此處為 './app.component.html'
。styleUrls
: 提供要套用在這個元件的樣式表的檔案位址與名稱。此處為 './app.component.css'
。The filter
property is of type union
, which means filter
could have the value of all
, active
, or done
.
-With the union
type, if you make a typo in the value you assign to the filter
property, TypeScript lets you know so that you can catch the bug early.
-This guide shows you how to add filtering in a later step, but you can also use a filter to show the default list of all the items.
這個 filter
屬性為 union
型態,表示 filter
的值可能為 all
、 active
,或者 done
。
+有了 union
型態,若您賦予 filter
屬性錯誤的值, TypeScript 會馬上讓您知道來及早除錯。
+本指引接下來的步驟會展示如何為您的列表加入過濾功能,但您仍可以使用過濾器來顯示這個列表預設的所有項目。
The allItems
array contains the to-do items and whether they are done
.
-The first item, eat
, has a done
value of true.
這個 allItems
陣列包含所有待辦項目,其中也包含項目是否已完成的 done
屬性。
+例如,第一個項目 eat
擁有值為 true 的 done
屬性。
The getter, get items()
, retrieves the items from the allItems
array if the filter
is equal to all
.
-Otherwise, get items()
returns the done
items or the outstanding items depending on how the user filters the view.
-The getter also establishes the name of the array as items
, which you'll use in the next section.
當 filter
等於 all
時,這個 getter get items()
會由 allItems
陣列中獲取所有的項目。
+否則, get items()
會回傳 done
的項目或是取決於使用者如何過濾的未完成項目。
+此 getter 也會建立名為 items
的陣列名稱,這部分您將會在下個章節使用到。