SlideShare a Scribd company logo
使用 TypeScript 駕馭
Web 世界的脫韁野馬
多奇數位創意有限公司
技術總監 黃保翕 ( Will 保哥 )
部落格:http://blog.miniasp.com/
以 Angular 2 開發框架為例
ANGULAR 2 簡介
Angular 2 Introduction
與 Angular 1.x 的比較
• 三大特色
– 效能改進 (Performance)
• 偵測變更:比 ng1 快 10 倍
• 渲染速度:比 ng1 快 5 倍 (Render & Re-render)
• 範本編譯:支援 Template 預先編譯機制
• 更小的 Library Size 與延遲載入機制
• 支援伺服器渲染機制 (Node.js & ASP.NET )
– 高生產力 (Productivity)
• 開發應用程式能夠用更簡潔的語法讓團隊更加容易上手跟維護
• 更強大的開發工具 Augury
• 移除超過 40+ 個 directives
– 多樣平台 (Versatility)
• 支援 Browser, Node.js, NativeScript, and more … 3
從框架轉向平台
4
Angular 2 的開發語言
TypeScript
ES 2016
ES 2015
ES5
5
Angular 2 的開發語言
• ES5
– 傳統 JavaScript 程式語言 ( IE9+ )
• ES 2015
– 此版本為 ES5 的「超集合」
– 具有新穎的 JavaScript 語言特性 ( let, const, for-of, … )
– 可透過 Babel 轉譯器將瀏覽器不支援的語法轉為 ES5 版本
• TypeScript
– 此版本為 ES 2015 的「超集合」
– 具有強型別特性、內建 ES5 轉譯器 (Transpiler)、更好的工具支
援
• Dart
– 非 JavaScript 家族的程式語言
– 具有強型別特性 6
Angular 2 的開發工具
• Visual Studio Code
• Visual Studio 2015
• Atom
• Sublime Text
• Plunker
7
建立第一個 ANGULAR 2 應用程式
Build your first Angular 2 Application
8
Angular 2 應用程式的組成
• App Component元件
• Child Component元件
• Services Component元件
• Pipe Component元件
Angular 2 頁面的組成
應用程式元件 + 樣板
( AppComponent + Templates )
頁首元件 + 樣板
( HeaderComponent )
子選單
元件 + 樣板
(SideComponent)
10
主要內容元件 + 樣板
(MainComponent)
Angular 2 結構剖析
11
Angular 2 結構剖析
• Module 應用程式被切分成許多「模組」
• Component 每個模組下有許多「元件」
• Template 每個元件都可能有自己的「樣板」
• Metadata 每個元件都可以標示「中繼資料」
• Data Binding 樣板與元件屬性、方法可以進行綁定
• Directive 將 DOM 轉換為多功能的「宣告命令」
• Service 由「服務」集中管理資料與運算邏輯
• Dependency Injection 由「相依注入」機制管理物件生命週期
12
Angular 2 元件的組成
範本
( Template )
• HTML 版面配置
• HTML 部分片段
• 資料繫結 (Bindings)
• 畫面命令 (Directives)
類別
( Class )
• 建構式 (Constructor)
• 屬性 (Properties)
• 方法 (Methods)
中繼資料
( Metadata )
• 裝飾器 (Decorator)
• 針對類別
• 針對屬性
• 針對方法
• 針對參數
13
手動建立基礎開發環境
1. 建立應用程式資料夾
2. 建立 tsconfig.json
3. 建立 package.json
4. 建立 typings.json
5. 建立 libraries & typings
6. 建立 index.html
7. 建立 main.ts (bootstrapper)
8. 設定 .gitignore 與建立版控
9. 設定 Visual Studio Code 開發工具 ( .vscode )
14
使用 Angular CLI 建立專案範本
• npm install -g angular-cli
• ng new PROJECT_NAME
• cd PROJECT_NAME
• ng serve
– http://localhost:4200/
• ng generate component my-new-component
縮寫語法:
ng g component my-new-component 15
複製現有的 Angular 2 專案範本
• 現有的專案範本
– miniasp/angular-2-boilerplate
– miniasp/angular-2-boilerplate-webpack
– AngularClass/angular2-webpack-starter
– angular/angular2-seed
– DanWahlin/Angular2-JumpStart
– johnpapa/angular2-tour-of-heroes
16
認識 ES 2015 / TypeScript 模組化技術
• 每個檔案都是一個「模組」( module )
• 每個模組內都可以「匯出」( export ) 公開的物件
• 匯出
export class Product {
pageTitle = "Hello World";
}
• 匯入
import { Product } from './product';
import { Product as prod } from './product';
import * as product from './product';
import './product'; 17
建立第一個元件 (根元件)
• 建立根元件 app/app.component.ts
export class AppComponent {
pageTitle: string = 'Hello World';
}
• 匯入 Angular 2 啟動器 app/main.ts
import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
bootstrap(AppComponent);
18
認識 Angular 2 元件的命名規則
// 命名規則: PascalCase
export class AppComponent {
// 命名規則: camelCase
pageTitle : string = "Hello World";
// 命名規則: 動詞 + 名詞 with camelCase
printTitle() {
console.log(this.pageTitle);
}
}
19
認識 ES7 / TypeScript 的 Decorator
• 可以套用在
– 類別
– 屬性
– 方法
– 方法中的參數
• 永遠以 @ 開頭
• 不用分號結尾
• 用法跟 C# 的 Attributes 很像!! 20
修改首頁 HTML 內容
• 修改 index.html
<my-app>
Loading App...
</my-app>
21
建立第一個元件的 HTML 範本
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<div>
<div class='container'><h1>{{pageTitle}}</h1></div>
</div>
`
})
export class AppComponent {
pageTitle: string = 'Hello World';
}
22
建立子元件
• 建立模組檔案 ( *.ts )
star.component.ts
• 建立元件類別 (Class) 與 中繼資料 (Decorator)
@Component({ selector: 'posts', template: `xxxxx` })
export class StarComponent {
}
• 設定元件範本 (Template)
star.component.html
• 套用結構式命令 (Structure Directives)
*ngIf='products && products.length'
*ngFor='let product of products' 23
載入子元件
• 修改上層元件的 directives
@Component({
templateUrl: 'app/products/product-list.component.html',
directives: [StarComponent]
})
• app/products/product-list.component.html
<ai-star [rating]='product.starRating'
(ratingClicked)='onRatingClicked($event)'>
</ai-star>
24
認識資料繫結方法 (Bindings)
• 內嵌繫結 ( interpolation )
{{property}}
• 屬性繫結 ( Property Binding )
[property]='statement'
• 事件繫結 ( Event Binding )
(event)='someMethod($event)'
• 雙向繫結 ( Two-way Binding )
[(ngModel)]='property'
25
透過 TypeScript 加強元件結構
• 使用型別標註 ( Type annotations ) 強化工具支援
• 使用「介面」強化工具支援 ( Strong typing )
• 使用「介面」確保 Lifecycle hooks 可以正確撰寫
• 使用泛型 ( Generic ) 建立程式碼範本
• 使用列舉 ( Enum ) 增加程式可讀性
26
相關連結
• Angular 2 官網
• Angular 2 學習資源
• Angular 2 風格指南 (官方版)
• Angular Augury (開發偵錯工具) (GitHub)
• codelyzer (程式碼風格分析器)
• Angular CLI (命令列工具)
• Angular 2 Cheat Sheet for TypeScript
• ng-conf 2016 – YouTube
• ReactiveX ( RxJS on GitHub )
• RxMarbles: Interactive diagrams of Rx Observables
• TypeScript - JavaScript that scales.
• TypeScript Handbook (中文版) 27
聯絡資訊
• The Will Will Web
記載著 Will 在網路世界的學習心得與技術分享
– http://blog.miniasp.com/
• Will 保哥的技術交流中心 (臉書粉絲專頁)
– http://www.facebook.com/will.fans
• Will 保哥的噗浪
– http://www.plurk.com/willh/invite
• Will 保哥的推特
– https://twitter.com/Will_Huang

More Related Content

PPTX
快快樂樂學 Angular 2 開發框架
PPTX
Angular 4 新手入門攻略完全制霸
PDF
우아하게 준비하는 테스트와 리팩토링 - PyCon Korea 2018
PDF
AI 연구자를 위한 클린코드 - GDG DevFest Seoul 2019
PPTX
d.ts 만들기
PPTX
Introduction à l’orienté objet en Python
PDF
GitHub 실습 교육
快快樂樂學 Angular 2 開發框架
Angular 4 新手入門攻略完全制霸
우아하게 준비하는 테스트와 리팩토링 - PyCon Korea 2018
AI 연구자를 위한 클린코드 - GDG DevFest Seoul 2019
d.ts 만들기
Introduction à l’orienté objet en Python
GitHub 실습 교육

What's hot (20)

PDF
Mise en oeuvre des framework de machines et deep learning v1
PDF
Conférence: Catalyseurs de l'Intelligence Artificielle et Écosystème des Fram...
PDF
Principles of Monitoring Microservices
PPTX
K8s in 3h - Kubernetes Fundamentals Training
PDF
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
PPTX
NashTech - Azure Application Insights
PDF
Support Java Avancé Troisième Partie
PDF
Transformation M2M avec ATL
PPT
Architecture des Systèmes Logiciels
PDF
Naver속도의, 속도에 의한, 속도를 위한 몽고DB (네이버 컨텐츠검색과 몽고DB) [Naver]
PDF
Architecture réparties et les services web
PDF
Support programmation orientée objet c# .net version f8
PDF
Was ist Docker?
ODP
Symfony Best Practices
PPTX
Introduction à spring boot
PPTX
Chp1- Introduction aux Technologies Web et SOA
PDF
Messaging in CQRS with MassTransit
PPTX
Introduction à Python
PPTX
Feature pyramid networks for object detection
PDF
Attention is all you need
Mise en oeuvre des framework de machines et deep learning v1
Conférence: Catalyseurs de l'Intelligence Artificielle et Écosystème des Fram...
Principles of Monitoring Microservices
K8s in 3h - Kubernetes Fundamentals Training
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
NashTech - Azure Application Insights
Support Java Avancé Troisième Partie
Transformation M2M avec ATL
Architecture des Systèmes Logiciels
Naver속도의, 속도에 의한, 속도를 위한 몽고DB (네이버 컨텐츠검색과 몽고DB) [Naver]
Architecture réparties et les services web
Support programmation orientée objet c# .net version f8
Was ist Docker?
Symfony Best Practices
Introduction à spring boot
Chp1- Introduction aux Technologies Web et SOA
Messaging in CQRS with MassTransit
Introduction à Python
Feature pyramid networks for object detection
Attention is all you need
Ad

Viewers also liked (20)

PPTX
從開發人員角度十分鐘理解區塊鏈技術
PPTX
[E2E Testing] 一鍵入手 E2E
PPTX
Breaking Down Bitcoin - Sean Walsh - Los Angeles
PPTX
Bitcoin Protocols 1.0 and 2.0 Explained in the Series: Blockchain: The Inform...
PDF
Bitcoin & Blockchain for Friends
PDF
What is Bitcoin - The Internet of Money
PDF
BlockChain, Bitcoin and Smart Contracts - Oleg Kudrenko
PPTX
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
PPTX
《保哥線上講堂》打造一個具有 Linux 溫度的 Windows 命令提示字元工具
PPTX
快速上手 Windows Containers 容器技術 (Docker Taipei)
PPTX
Azure Web App on Linux @ Global Azure Bootcamp 2017 Taiwan
PPTX
Growth Mindset 經驗分享
PPTX
git merge 與 rebase 的觀念與實務應用
PPTX
ASP.NET 5 的創新與變革
PPTX
中小企業選擇雲端服務的實戰密技
PPTX
DEV305 - ASP.NET 5 開發攻略
PPTX
初學者都該了解的 HTTP 通訊協定基礎
PPTX
SQL Server 資料庫版本控管
PPTX
簡介 Git hub 平台 ( 1.5 hrs )
PPTX
Visual Studio 2017 新功能探索 (Study4.TW)
從開發人員角度十分鐘理解區塊鏈技術
[E2E Testing] 一鍵入手 E2E
Breaking Down Bitcoin - Sean Walsh - Los Angeles
Bitcoin Protocols 1.0 and 2.0 Explained in the Series: Blockchain: The Inform...
Bitcoin & Blockchain for Friends
What is Bitcoin - The Internet of Money
BlockChain, Bitcoin and Smart Contracts - Oleg Kudrenko
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
《保哥線上講堂》打造一個具有 Linux 溫度的 Windows 命令提示字元工具
快速上手 Windows Containers 容器技術 (Docker Taipei)
Azure Web App on Linux @ Global Azure Bootcamp 2017 Taiwan
Growth Mindset 經驗分享
git merge 與 rebase 的觀念與實務應用
ASP.NET 5 的創新與變革
中小企業選擇雲端服務的實戰密技
DEV305 - ASP.NET 5 開發攻略
初學者都該了解的 HTTP 通訊協定基礎
SQL Server 資料庫版本控管
簡介 Git hub 平台 ( 1.5 hrs )
Visual Studio 2017 新功能探索 (Study4.TW)
Ad

Similar to 使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例 (20)

PPTX
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
PPTX
Angular 7 全新功能探索 (Angular Taiwan 2018)
PPTX
ASP.NET MVC 6 新功能探索
PPTX
ASP.NET Core 6.0 全新功能探索
PPT
Kindeditor 设计思路
PDF
Real World ASP.NET MVC
PPTX
微服務架構 導入經驗分享 吳剛志 - Community Open Camp
PDF
合久必分,分久必合
PDF
Kind editor设计思路
PDF
浏览器渲染与web前端开发
PPTX
從頭打造 C#、.NET 與 ASP.NET Core 開發環境
PDF
KISSY Editor Design 2
PDF
Mvc model
PPTX
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
PPTX
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛
PDF
ASP.NET Core 2.1設計新思維與新發展
PDF
Kindeditor设计思路v2
PDF
美团前端架构简介
PDF
钱宝坤:多浏览器集成的JavaScript单元测试工具
PPTX
ASP.Net MVC Framework
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
Angular 7 全新功能探索 (Angular Taiwan 2018)
ASP.NET MVC 6 新功能探索
ASP.NET Core 6.0 全新功能探索
Kindeditor 设计思路
Real World ASP.NET MVC
微服務架構 導入經驗分享 吳剛志 - Community Open Camp
合久必分,分久必合
Kind editor设计思路
浏览器渲染与web前端开发
從頭打造 C#、.NET 與 ASP.NET Core 開發環境
KISSY Editor Design 2
Mvc model
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛
ASP.NET Core 2.1設計新思維與新發展
Kindeditor设计思路v2
美团前端架构简介
钱宝坤:多浏览器集成的JavaScript单元测试工具
ASP.Net MVC Framework

More from Will Huang (20)

PPTX
深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
PPTX
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
PPTX
你一定不能不知道的 Markdown 寫作技巧
PPTX
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
PPTX
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
PPTX
Micro-frontends with Angular 10 (Modern Web 2020)
PPTX
從實戰經驗看到的 K8S 導入痛點
PPTX
RxJS 6 新手入門
PPTX
极速 Angular 开发:效能调校技巧 (ngChina 2019)
PPTX
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
PPTX
Protractor: The Hacker way (NG-MY 2019)
PPTX
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
PPTX
Angular 开发技巧 (2018 ngChina 开发者大会)
PPTX
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
PPTX
AKS 與開發人員體驗 (Kubernetes 大講堂)
PPTX
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
PPTX
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
PPTX
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
PPTX
以敏捷架構打造美國軟體外包專案的經驗談
PPTX
開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018
深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
你一定不能不知道的 Markdown 寫作技巧
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
Micro-frontends with Angular 10 (Modern Web 2020)
從實戰經驗看到的 K8S 導入痛點
RxJS 6 新手入門
极速 Angular 开发:效能调校技巧 (ngChina 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
Protractor: The Hacker way (NG-MY 2019)
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
Angular 开发技巧 (2018 ngChina 开发者大会)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
AKS 與開發人員體驗 (Kubernetes 大講堂)
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
以敏捷架構打造美國軟體外包專案的經驗談
開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018

使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例

  • 1. 使用 TypeScript 駕馭 Web 世界的脫韁野馬 多奇數位創意有限公司 技術總監 黃保翕 ( Will 保哥 ) 部落格:http://blog.miniasp.com/ 以 Angular 2 開發框架為例
  • 2. ANGULAR 2 簡介 Angular 2 Introduction
  • 3. 與 Angular 1.x 的比較 • 三大特色 – 效能改進 (Performance) • 偵測變更:比 ng1 快 10 倍 • 渲染速度:比 ng1 快 5 倍 (Render & Re-render) • 範本編譯:支援 Template 預先編譯機制 • 更小的 Library Size 與延遲載入機制 • 支援伺服器渲染機制 (Node.js & ASP.NET ) – 高生產力 (Productivity) • 開發應用程式能夠用更簡潔的語法讓團隊更加容易上手跟維護 • 更強大的開發工具 Augury • 移除超過 40+ 個 directives – 多樣平台 (Versatility) • 支援 Browser, Node.js, NativeScript, and more … 3
  • 6. Angular 2 的開發語言 • ES5 – 傳統 JavaScript 程式語言 ( IE9+ ) • ES 2015 – 此版本為 ES5 的「超集合」 – 具有新穎的 JavaScript 語言特性 ( let, const, for-of, … ) – 可透過 Babel 轉譯器將瀏覽器不支援的語法轉為 ES5 版本 • TypeScript – 此版本為 ES 2015 的「超集合」 – 具有強型別特性、內建 ES5 轉譯器 (Transpiler)、更好的工具支 援 • Dart – 非 JavaScript 家族的程式語言 – 具有強型別特性 6
  • 7. Angular 2 的開發工具 • Visual Studio Code • Visual Studio 2015 • Atom • Sublime Text • Plunker 7
  • 8. 建立第一個 ANGULAR 2 應用程式 Build your first Angular 2 Application 8
  • 9. Angular 2 應用程式的組成 • App Component元件 • Child Component元件 • Services Component元件 • Pipe Component元件
  • 10. Angular 2 頁面的組成 應用程式元件 + 樣板 ( AppComponent + Templates ) 頁首元件 + 樣板 ( HeaderComponent ) 子選單 元件 + 樣板 (SideComponent) 10 主要內容元件 + 樣板 (MainComponent)
  • 12. Angular 2 結構剖析 • Module 應用程式被切分成許多「模組」 • Component 每個模組下有許多「元件」 • Template 每個元件都可能有自己的「樣板」 • Metadata 每個元件都可以標示「中繼資料」 • Data Binding 樣板與元件屬性、方法可以進行綁定 • Directive 將 DOM 轉換為多功能的「宣告命令」 • Service 由「服務」集中管理資料與運算邏輯 • Dependency Injection 由「相依注入」機制管理物件生命週期 12
  • 13. Angular 2 元件的組成 範本 ( Template ) • HTML 版面配置 • HTML 部分片段 • 資料繫結 (Bindings) • 畫面命令 (Directives) 類別 ( Class ) • 建構式 (Constructor) • 屬性 (Properties) • 方法 (Methods) 中繼資料 ( Metadata ) • 裝飾器 (Decorator) • 針對類別 • 針對屬性 • 針對方法 • 針對參數 13
  • 14. 手動建立基礎開發環境 1. 建立應用程式資料夾 2. 建立 tsconfig.json 3. 建立 package.json 4. 建立 typings.json 5. 建立 libraries & typings 6. 建立 index.html 7. 建立 main.ts (bootstrapper) 8. 設定 .gitignore 與建立版控 9. 設定 Visual Studio Code 開發工具 ( .vscode ) 14
  • 15. 使用 Angular CLI 建立專案範本 • npm install -g angular-cli • ng new PROJECT_NAME • cd PROJECT_NAME • ng serve – http://localhost:4200/ • ng generate component my-new-component 縮寫語法: ng g component my-new-component 15
  • 16. 複製現有的 Angular 2 專案範本 • 現有的專案範本 – miniasp/angular-2-boilerplate – miniasp/angular-2-boilerplate-webpack – AngularClass/angular2-webpack-starter – angular/angular2-seed – DanWahlin/Angular2-JumpStart – johnpapa/angular2-tour-of-heroes 16
  • 17. 認識 ES 2015 / TypeScript 模組化技術 • 每個檔案都是一個「模組」( module ) • 每個模組內都可以「匯出」( export ) 公開的物件 • 匯出 export class Product { pageTitle = "Hello World"; } • 匯入 import { Product } from './product'; import { Product as prod } from './product'; import * as product from './product'; import './product'; 17
  • 18. 建立第一個元件 (根元件) • 建立根元件 app/app.component.ts export class AppComponent { pageTitle: string = 'Hello World'; } • 匯入 Angular 2 啟動器 app/main.ts import { bootstrap } from '@angular/platform-browser-dynamic'; import { AppComponent } from './app.component'; bootstrap(AppComponent); 18
  • 19. 認識 Angular 2 元件的命名規則 // 命名規則: PascalCase export class AppComponent { // 命名規則: camelCase pageTitle : string = "Hello World"; // 命名規則: 動詞 + 名詞 with camelCase printTitle() { console.log(this.pageTitle); } } 19
  • 20. 認識 ES7 / TypeScript 的 Decorator • 可以套用在 – 類別 – 屬性 – 方法 – 方法中的參數 • 永遠以 @ 開頭 • 不用分號結尾 • 用法跟 C# 的 Attributes 很像!! 20
  • 21. 修改首頁 HTML 內容 • 修改 index.html <my-app> Loading App... </my-app> 21
  • 22. 建立第一個元件的 HTML 範本 import { Component } from '@angular/core'; @Component({ selector: 'my-app', template: ` <div> <div class='container'><h1>{{pageTitle}}</h1></div> </div> ` }) export class AppComponent { pageTitle: string = 'Hello World'; } 22
  • 23. 建立子元件 • 建立模組檔案 ( *.ts ) star.component.ts • 建立元件類別 (Class) 與 中繼資料 (Decorator) @Component({ selector: 'posts', template: `xxxxx` }) export class StarComponent { } • 設定元件範本 (Template) star.component.html • 套用結構式命令 (Structure Directives) *ngIf='products && products.length' *ngFor='let product of products' 23
  • 24. 載入子元件 • 修改上層元件的 directives @Component({ templateUrl: 'app/products/product-list.component.html', directives: [StarComponent] }) • app/products/product-list.component.html <ai-star [rating]='product.starRating' (ratingClicked)='onRatingClicked($event)'> </ai-star> 24
  • 25. 認識資料繫結方法 (Bindings) • 內嵌繫結 ( interpolation ) {{property}} • 屬性繫結 ( Property Binding ) [property]='statement' • 事件繫結 ( Event Binding ) (event)='someMethod($event)' • 雙向繫結 ( Two-way Binding ) [(ngModel)]='property' 25
  • 26. 透過 TypeScript 加強元件結構 • 使用型別標註 ( Type annotations ) 強化工具支援 • 使用「介面」強化工具支援 ( Strong typing ) • 使用「介面」確保 Lifecycle hooks 可以正確撰寫 • 使用泛型 ( Generic ) 建立程式碼範本 • 使用列舉 ( Enum ) 增加程式可讀性 26
  • 27. 相關連結 • Angular 2 官網 • Angular 2 學習資源 • Angular 2 風格指南 (官方版) • Angular Augury (開發偵錯工具) (GitHub) • codelyzer (程式碼風格分析器) • Angular CLI (命令列工具) • Angular 2 Cheat Sheet for TypeScript • ng-conf 2016 – YouTube • ReactiveX ( RxJS on GitHub ) • RxMarbles: Interactive diagrams of Rx Observables • TypeScript - JavaScript that scales. • TypeScript Handbook (中文版) 27
  • 28. 聯絡資訊 • The Will Will Web 記載著 Will 在網路世界的學習心得與技術分享 – http://blog.miniasp.com/ • Will 保哥的技術交流中心 (臉書粉絲專頁) – http://www.facebook.com/will.fans • Will 保哥的噗浪 – http://www.plurk.com/willh/invite • Will 保哥的推特 – https://twitter.com/Will_Huang