SlideShare a Scribd company logo
Angular 4 新手入門攻略完全制霸
多奇數位創意有限公司
技術總監 黃保翕 ( Will 保哥 )
部落格:http://blog.miniasp.com/
帶你認識 Angular 4 開發流程
錄影重播
http://bit.ly/ng4intro
建立 ANGULAR 應用程式
Build your first Angular Application
3
使用 Angular CLI 建立專案架構
• 使用說明
– ng --help
• 建立新專案並啟動開發伺服器
– ng new PROJECT_NAME
– cd PROJECT_NAME
– npm start ( 執行 ng serve 也可以 )
– http://localhost:4200
• 啟動開發伺服器並自動開啟瀏覽器
– ng serve --open
• 指定不同埠號啟動開發伺服器
– ng serve --port 4201
了解 Angular CLI 建立的專案結構
• 首頁 HTML 與 Angular 主程式
– src/index.html 預設網站首頁 ( 還是要有一份 HTML 網頁來載入 JS 執行 )
– src/style.css 預設全站共用的 CSS 樣式檔
– src/main.ts 預設 Angular 程式進入點
• 公用檔案資料夾
– src/assets/ 網站相關的靜態資源檔案 (CSS, Image, Fonts, …)
• 應用程式原始碼
– src/app/app.module.ts 應用程式的全域模組 (AppModule)
– src/app/app.component.ts 根元件主程式 (AppComponent)
– src/app/app.component.html 根元件範本檔 (HTML)
– src/app/app.component.css 根元件樣式檔 (CSS)
– src/app/app.component.spec.ts 根元件單元測試程式
• 共用的環境變數
– src/environments/environment.ts 環境變數設定 ( 預設 )
– src/environments/environment.prod.ts 環境變數設定 ( ng build --env=prod )
src/index.html
6
根元件的 directive 宣告
咦?沒有載入任何 JavaScript 函式庫?
src/main.ts
7
啟用 Production 模式 (提升執行速度)
設定 AppModule 為啟動模組
src/app/app.module.ts
宣告跟 View 有關的元件
宣告要匯入此模組的外部模組
宣告要註冊的服務元件
宣告根元件
src/app/app.component.ts
9
指令 (directive) 選擇器
元件網頁範本
元件 CSS 樣式
TypeScript 類別
類別中的屬性 (Property)
類別中的方法 (Method)
類別的建構式
認識 Angular 元件的命名規則
// 命名規則: PascalCase
export class AppComponent {
// 命名規則: camelCase
pageTitle : string = "Hello World";
// 命名規則: 動詞 + 名詞 with camelCase
printTitle() {
console.log(this.pageTitle);
}
}
10
Angular 應用程式的組成
• App Component元件
• Child Component元件
• Services Component元件
• Pipe Component元件
模組 • AppModule
Angular 頁面的組成
應用程式元件 + 樣板 + 樣式
( AppComponent )
頁首元件 + 樣板 + 樣式
( HeaderComponent )
子選單
元件 + 樣板 + 樣式
(AsideComponent)
12
主要內容
元件 + 樣板 + 樣式
(ArticleComponent)
Angular 元件的組成
範本
( Template )
• HTML 版面配置
• HTML 部分片段
• 資料繫結 (Bindings)
• 畫面命令 (Directives)
類別
( Class )
• 建構式 (Constructor)
• 屬性 (Properties)
• 方法 (Methods)
中繼資料
( Metadata )
• 裝飾器 (Decorator)
• 針對類別
• 針對屬性
• 針對方法
• 針對參數
13
認識 Angular 元件的程式碼結構
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.css']
})
export class AppComponent {
title = 'app works!';
}
14
類別
匯入模組
裝飾器
使用 Angular CLI 快速產生程式碼
• 透過 藍圖 (blueprint) 產生程式碼
– ng generate 藍圖 元件名稱
• 透過 藍圖 (blueprint) 產生程式碼 (簡寫)
– ng g 藍圖 元件名稱
• 使用範例
– 產生 HeaderComponent 元件
• ng g component header # 建立元件
• ng g c header # 簡寫版本 ( c = component )
• ng g c header --spec=false # 不要建立單元測試檔 ( *.spec.ts )
• ng g c charts/header # 在特定目錄(模組)下建立元件
– 產生 DataService 服務元件
• ng g s data # 建立服務元件
– 產生 Charts 模組
• ng g m charts # 建立模組
– 查詢其他藍圖用法
• ng g --help # 顯示所有藍圖與用法說明
常見 Angular CLI 產生器藍圖與範例
16
藍圖名稱 使用方式
Component ng g component my-new-component
Service ng g service my-new-service
Module ng g module my-module
Directive ng g directive my-new-directive
Pipe ng g pipe my-new-pipe
Class ng g class my-new-class
Interface ng g interface my-new-interface
Enum ng g enum my-new-enum
建立子元件 ( Child Component )
• 建立子元件
– 透過 ng generate component header 建立元件
– 簡寫指令:ng g c header --spec=false
– 會建立 HeaderComponent 元件類別
• 會自動在 app.module.ts 匯入 declarations 宣告
– import { HeaderComponent } from './header/header.component';
• 在任意元件的範本中使用 Directives 語法載入元件
– <app-header></app-header>
17
資料繫結的四種方法 (Binding syntax)
• 內嵌繫結 ( interpolation )
{{property}}
• 屬性繫結 ( Property Binding )
[propertyName]="statement"
bind-propertyName="expression"
[attr.attributeName]="statement"
bind-attr.attributeName="expression"
• 事件繫結 ( Event Binding )
(eventName)="someMethod($event)"
on-eventName="someMethod($event)"
• 雙向繫結 ( Two-way Binding )
[(ngModel)]='property'
bindon-ngModel='property' 18
相關連結
• 台灣 Angular 開發者社群 (Facebook)
• 台灣 Angular 技術論壇 (Forum)
• Angular 官網 ( 官方簡體中文翻譯 )
• Angular 風格指南 (官方版)
• Angular 學習資源 (官方版)
• Angular 學習資源 (社群版)
• Awesome Angular
• Angular 2 Fundamentals | AngularClass (免費 Angular 線上課程)
• 前端工程的夢幻逸品:Angular 2 開發框架介紹
聯絡資訊
• The Will Will Web
記載著 Will 在網路世界的學習心得與技術分享
– http://blog.miniasp.com/
• Will 保哥的技術交流中心 (臉書粉絲專頁)
– http://www.facebook.com/will.fans
• Will 保哥的推特
– https://twitter.com/Will_Huang
• Will 保哥的噗浪
– http://www.plurk.com/willh/invite

More Related Content

What's hot (20)

PDF
Tech Talk on ReactJS
Atlogys Technical Consulting
 
PDF
Angular material
Kalpesh Satasiya
 
PPTX
Selenium with java
Gousalya Ramachandran
 
PPT
Selenium
tanvir afzal
 
PDF
SPA Editor - Adobe Experience Manager Sites
Gabriel Walt
 
PPTX
Angular 9
Raja Vishnu
 
PPTX
QA Challenge Accepted 4.0 - Cypress vs. Selenium
Lyudmil Latinov
 
PDF
e3f55595181f7cad006f26db820fb78ec146e00e-1646623528083 (1).pdf
SILVIUSyt
 
PDF
Angular - Chapter 3 - Components
WebStackAcademy
 
PPTX
Browser Automation with Playwright – for integration, RPA, UI testing and mor...
Lucas Jellema
 
PDF
Introduction to Firebase with Android and Beyond...
Kasper Loevborg Jensen
 
PDF
Spring Framework
NexThoughts Technologies
 
PPTX
Laravel development (Laravel History, Environment Setup & Laravel Installatio...
Dilouar Hossain
 
PDF
Playwright Begginers Presentation
FranPerea6
 
PDF
Selenide
DataArt
 
PPTX
Mern stack developement
kalyankumar836878
 
PPTX
Postman Introduction
Rahul Agarwal
 
PPTX
Full stack devlopment using django main ppt
SudhanshuVijay3
 
PPTX
Fitness App ppt
Azmeen Gadit
 
PDF
Understanding MicroSERVICE Architecture with Java & Spring Boot
Kashif Ali Siddiqui
 
Tech Talk on ReactJS
Atlogys Technical Consulting
 
Angular material
Kalpesh Satasiya
 
Selenium with java
Gousalya Ramachandran
 
Selenium
tanvir afzal
 
SPA Editor - Adobe Experience Manager Sites
Gabriel Walt
 
Angular 9
Raja Vishnu
 
QA Challenge Accepted 4.0 - Cypress vs. Selenium
Lyudmil Latinov
 
e3f55595181f7cad006f26db820fb78ec146e00e-1646623528083 (1).pdf
SILVIUSyt
 
Angular - Chapter 3 - Components
WebStackAcademy
 
Browser Automation with Playwright – for integration, RPA, UI testing and mor...
Lucas Jellema
 
Introduction to Firebase with Android and Beyond...
Kasper Loevborg Jensen
 
Spring Framework
NexThoughts Technologies
 
Laravel development (Laravel History, Environment Setup & Laravel Installatio...
Dilouar Hossain
 
Playwright Begginers Presentation
FranPerea6
 
Selenide
DataArt
 
Mern stack developement
kalyankumar836878
 
Postman Introduction
Rahul Agarwal
 
Full stack devlopment using django main ppt
SudhanshuVijay3
 
Fitness App ppt
Azmeen Gadit
 
Understanding MicroSERVICE Architecture with Java & Spring Boot
Kashif Ali Siddiqui
 

Similar to Angular 4 新手入門攻略完全制霸 (8)

PPTX
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
Chieh Kai Yang
 
PPTX
Angular 7 全新功能探索 (Angular Taiwan 2018)
Will Huang
 
PPTX
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
Will Huang
 
PPTX
快快樂樂學 Angular 2 開發框架
Will Huang
 
PPTX
Angular 4 網站開發最佳實務 (Modern Web 2017)
Will Huang
 
PDF
Schematics 實戰
志龍 陳
 
PPTX
從前端設計的角度來看 Angular - TW2018 amos
Amos Lee
 
PDF
Angular從入門到實戰(二)
志龍 陳
 
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
Chieh Kai Yang
 
Angular 7 全新功能探索 (Angular Taiwan 2018)
Will Huang
 
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
Will Huang
 
快快樂樂學 Angular 2 開發框架
Will Huang
 
Angular 4 網站開發最佳實務 (Modern Web 2017)
Will Huang
 
Schematics 實戰
志龍 陳
 
從前端設計的角度來看 Angular - TW2018 amos
Amos Lee
 
Angular從入門到實戰(二)
志龍 陳
 
Ad

More from Will Huang (20)

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

Angular 4 新手入門攻略完全制霸

  • 1. Angular 4 新手入門攻略完全制霸 多奇數位創意有限公司 技術總監 黃保翕 ( Will 保哥 ) 部落格:http://blog.miniasp.com/ 帶你認識 Angular 4 開發流程
  • 3. 建立 ANGULAR 應用程式 Build your first Angular Application 3
  • 4. 使用 Angular CLI 建立專案架構 • 使用說明 – ng --help • 建立新專案並啟動開發伺服器 – ng new PROJECT_NAME – cd PROJECT_NAME – npm start ( 執行 ng serve 也可以 ) – http://localhost:4200 • 啟動開發伺服器並自動開啟瀏覽器 – ng serve --open • 指定不同埠號啟動開發伺服器 – ng serve --port 4201
  • 5. 了解 Angular CLI 建立的專案結構 • 首頁 HTML 與 Angular 主程式 – src/index.html 預設網站首頁 ( 還是要有一份 HTML 網頁來載入 JS 執行 ) – src/style.css 預設全站共用的 CSS 樣式檔 – src/main.ts 預設 Angular 程式進入點 • 公用檔案資料夾 – src/assets/ 網站相關的靜態資源檔案 (CSS, Image, Fonts, …) • 應用程式原始碼 – src/app/app.module.ts 應用程式的全域模組 (AppModule) – src/app/app.component.ts 根元件主程式 (AppComponent) – src/app/app.component.html 根元件範本檔 (HTML) – src/app/app.component.css 根元件樣式檔 (CSS) – src/app/app.component.spec.ts 根元件單元測試程式 • 共用的環境變數 – src/environments/environment.ts 環境變數設定 ( 預設 ) – src/environments/environment.prod.ts 環境變數設定 ( ng build --env=prod )
  • 7. src/main.ts 7 啟用 Production 模式 (提升執行速度) 設定 AppModule 為啟動模組
  • 9. src/app/app.component.ts 9 指令 (directive) 選擇器 元件網頁範本 元件 CSS 樣式 TypeScript 類別 類別中的屬性 (Property) 類別中的方法 (Method) 類別的建構式
  • 10. 認識 Angular 元件的命名規則 // 命名規則: PascalCase export class AppComponent { // 命名規則: camelCase pageTitle : string = "Hello World"; // 命名規則: 動詞 + 名詞 with camelCase printTitle() { console.log(this.pageTitle); } } 10
  • 11. Angular 應用程式的組成 • App Component元件 • Child Component元件 • Services Component元件 • Pipe Component元件 模組 • AppModule
  • 12. Angular 頁面的組成 應用程式元件 + 樣板 + 樣式 ( AppComponent ) 頁首元件 + 樣板 + 樣式 ( HeaderComponent ) 子選單 元件 + 樣板 + 樣式 (AsideComponent) 12 主要內容 元件 + 樣板 + 樣式 (ArticleComponent)
  • 13. Angular 元件的組成 範本 ( Template ) • HTML 版面配置 • HTML 部分片段 • 資料繫結 (Bindings) • 畫面命令 (Directives) 類別 ( Class ) • 建構式 (Constructor) • 屬性 (Properties) • 方法 (Methods) 中繼資料 ( Metadata ) • 裝飾器 (Decorator) • 針對類別 • 針對屬性 • 針對方法 • 針對參數 13
  • 14. 認識 Angular 元件的程式碼結構 import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: 'app.component.html', styleUrls: ['app.component.css'] }) export class AppComponent { title = 'app works!'; } 14 類別 匯入模組 裝飾器
  • 15. 使用 Angular CLI 快速產生程式碼 • 透過 藍圖 (blueprint) 產生程式碼 – ng generate 藍圖 元件名稱 • 透過 藍圖 (blueprint) 產生程式碼 (簡寫) – ng g 藍圖 元件名稱 • 使用範例 – 產生 HeaderComponent 元件 • ng g component header # 建立元件 • ng g c header # 簡寫版本 ( c = component ) • ng g c header --spec=false # 不要建立單元測試檔 ( *.spec.ts ) • ng g c charts/header # 在特定目錄(模組)下建立元件 – 產生 DataService 服務元件 • ng g s data # 建立服務元件 – 產生 Charts 模組 • ng g m charts # 建立模組 – 查詢其他藍圖用法 • ng g --help # 顯示所有藍圖與用法說明
  • 16. 常見 Angular CLI 產生器藍圖與範例 16 藍圖名稱 使用方式 Component ng g component my-new-component Service ng g service my-new-service Module ng g module my-module Directive ng g directive my-new-directive Pipe ng g pipe my-new-pipe Class ng g class my-new-class Interface ng g interface my-new-interface Enum ng g enum my-new-enum
  • 17. 建立子元件 ( Child Component ) • 建立子元件 – 透過 ng generate component header 建立元件 – 簡寫指令:ng g c header --spec=false – 會建立 HeaderComponent 元件類別 • 會自動在 app.module.ts 匯入 declarations 宣告 – import { HeaderComponent } from './header/header.component'; • 在任意元件的範本中使用 Directives 語法載入元件 – <app-header></app-header> 17
  • 18. 資料繫結的四種方法 (Binding syntax) • 內嵌繫結 ( interpolation ) {{property}} • 屬性繫結 ( Property Binding ) [propertyName]="statement" bind-propertyName="expression" [attr.attributeName]="statement" bind-attr.attributeName="expression" • 事件繫結 ( Event Binding ) (eventName)="someMethod($event)" on-eventName="someMethod($event)" • 雙向繫結 ( Two-way Binding ) [(ngModel)]='property' bindon-ngModel='property' 18
  • 19. 相關連結 • 台灣 Angular 開發者社群 (Facebook) • 台灣 Angular 技術論壇 (Forum) • Angular 官網 ( 官方簡體中文翻譯 ) • Angular 風格指南 (官方版) • Angular 學習資源 (官方版) • Angular 學習資源 (社群版) • Awesome Angular • Angular 2 Fundamentals | AngularClass (免費 Angular 線上課程) • 前端工程的夢幻逸品:Angular 2 開發框架介紹
  • 20. 聯絡資訊 • The Will Will Web 記載著 Will 在網路世界的學習心得與技術分享 – http://blog.miniasp.com/ • Will 保哥的技術交流中心 (臉書粉絲專頁) – http://www.facebook.com/will.fans • Will 保哥的推特 – https://twitter.com/Will_Huang • Will 保哥的噗浪 – http://www.plurk.com/willh/invite