91
AngularJS 2014 - 達暉資訊 - 教育訓練系列課程

AngularJS training in Luster

Embed Size (px)

DESCRIPTION

2014 達暉資訊教育訓練課程 - AngularJS 課程簡介: 想要開發複雜的互動網頁,卻被拉哩拉咂的 JS 和 HTML 搞得暈頭轉向? 還在為處理大量的動態資料顯示感到厭煩? 想猶如造物主般的自己創造自己的 HTML 標籤? 來試試 AngularJS 吧 ~ *MVVM 讓你的 Code 清爽又乾淨。 *2-way Data Binding 保證你藥到病除。 *Directive 讓你擴展 HTML,享受自在的樂趣。 AngularJS 是一個極其優美的前端架構,使用它,讓你的開發路更有趣,更有成就感~

Citation preview

Page 1: AngularJS training in Luster

AngularJS!

2014 - 達暉資訊 - 教育訓練系列課程

Page 2: AngularJS training in Luster

Me - 鐘友志 Jason Chung

- 軟體工程師 (達暉資訊有限公司)

- [email protected]

Page 3: AngularJS training in Luster

What is it

1. by

2. JavaScript MVW FrameworkModel-View-Whatever

Page 4: AngularJS training in Luster

Observer

About MVC

ViewUI, Represents

current model state

ControllerMake decision for

the View

ModelDomain-specific data

User Input

Modify

Display Output

Notify

Handle event

Page 5: AngularJS training in Luster

About MVVMView

User Interface

View ModelState and Operations

Data bindings and

Commands

Send notifications

Update

ModelDomain-specific data

Send notifications

Page 6: AngularJS training in Luster

ViewUser Interface

ModelDomain-specific data

About MVW

Whatever works for you

Page 7: AngularJS training in Luster

它改變了什麼?

Page 8: AngularJS training in Luster

Let’s coding for !

Hello world

Page 9: AngularJS training in Luster

Hello world in JS

Page 10: AngularJS training in Luster

Hello world in jQuery

Page 11: AngularJS training in Luster

What’s the problem in JS & jQuery?(多選)

1. 當需求增加,程式碼可能重複

2. 難以得知某個 HTML element 的所有⾏行為

3. 輸入了 “Hello, ...”,無法同時顯⽰示在不同的 <DIV> 上

4. 改變了某個 HTML element 的 id 或 class,可能照成無法預期的錯誤

5. 使⽤用 jQuery 的 selector 很難 debug

Page 12: AngularJS training in Luster

Can not<div id=“output”>$(“#input”).keyup(function () { $(“#output”).html(…); });

<input id=“input”>

<div id=“output1”>$(“#input1”).keyup(function () { $(“#output1”).html(…); });

<input id=“input1”>

<div id=“output2”>$(“#input2”).keyup(function () { $(“#output2”).html(…); });

<input id=“input2”>

Page 13: AngularJS training in Luster

$(“#input1”).keyup(function () { var val = $(this).val(); ! $(“#output1”).html(val); !});

<div id=“output1”><input id=“input1”>

如果需求改變...

Page 14: AngularJS training in Luster

<div id=“output1”>

$(“#input1”).keyup(function () { var val = $(this).val(); ! $(“#output1”).html(val); !});

<input id=“input1”>

<input id=“input2”>

$(“#input2”).keyup(function () { var val = $(this).val(); ! $(“#input1”).val(val) $(“#output1”).html(val); });

$(“#input2”).val(val);

如果需求改變...

Page 15: AngularJS training in Luster

input1

Value

output1

Page 16: AngularJS training in Luster

input2

input1

Value

output1

output2

Page 17: AngularJS training in Luster

input2

input1

Value

output1

output2

input3

input4

…WTF

Page 18: AngularJS training in Luster

當 HTML 跟 JS 分開時...

??

Page 19: AngularJS training in Luster

What’s the problem in jQuery

維護 id/class 在幹嘛?加班!1.

到底為了什麼維護 id/class?

2. 操作 DOM,因為要處理很多的 UI 互動

有時候無法從 HTML 中,看出元素的行為3.

Page 20: AngularJS training in Luster

Hello world in AngularJS

Page 21: AngularJS training in Luster

幫我們做了什麼?

Page 22: AngularJS training in Luster
Page 23: AngularJS training in Luster
Page 24: AngularJS training in Luster

What’s So Good in AngularJS

·• 三個步驟,搞定!

·• Wow, no JS!

·• Readable!

Page 25: AngularJS training in Luster

VSTwo way data bindingDOM manipulation

我們必須處理很多 event 我們專注 data

編程式的開發思維 宣告式的開發思維

很多很棒的 function 適合處理資料與 UI 的互動

IE 支援 6 以上 IE 支援 8 以上

Page 26: AngularJS training in Luster

So, Why we use it ?

程式碼更簡潔,更易讀!

我們只需要注意 data!

還有,不用加班!

Two way data binding 讓我們

不再關心 DOM 的操作

Page 27: AngularJS training in Luster

進入 AngularJS 的世界

Page 28: AngularJS training in Luster

很多的 Concepts

Scopes

Controllers Services

Directives

Dependency Injection

etc…RouteTemplate

Module

Promise

Page 29: AngularJS training in Luster

所以,我們從...

Page 30: AngularJS training in Luster

簡單的 Web App 開始如果能現學現賣,那就理想了

Page 31: AngularJS training in Luster

那,要做什麼呢?

Page 32: AngularJS training in Luster

男生居多,那就 妹

·•差不多是這樣貼心提醒:別瀏覽太多次,每月連線次數有限!

·•請自行更換主題,例如:暗黑版、帥哥版...

http://ilovehotty.parseapp.com

Page 33: AngularJS training in Luster

·•Topic:我愛正妹

• Requirement:

1. 簡單的清單(列出所有追蹤中的正妹)

2. 可關鍵字搜尋

3. 可排序(根據喜愛的程度)

4. CRUD(人的喜好,總是會隨時間改變)

Page 34: AngularJS training in Luster

Let’s Coding for 我愛正妹

Round 1

Page 35: AngularJS training in Luster

ng-app、ng-controller…

到底是什麼東西?

ng-model、ng-repeat、ng-click…

Page 36: AngularJS training in Luster

Directives讓我們有能力新增自己的 HTML 語法

Page 37: AngularJS training in Luster

原 HTML

Browser 幫我們做的事

<ul><li><audio>

Page 38: AngularJS training in Luster

如果你想要

那麼, 這就是 Directive 要幫我們解決的事!

Page 39: AngularJS training in Luster

Directives 其實是

= Template (HTML)

Behavior (JavaScript)+

Page 40: AngularJS training in Luster

之後,我們就可以

Ctrl+C<weather location=“local”></weather>

<weather location=“taipei”></weather>Ctrl+V

<weather location=“Kaohsiung”></weather>

Ctrl+V

<weather location=“somewhere”></weather>Ctrl+V

目前所在地

臺北

高雄

Page 41: AngularJS training in Luster

而且以後的我們,

只要維護一份

HTML (Template)

JavaScript (Behavior)

Code

Page 42: AngularJS training in Luster

Directives 小結

·•封裝 複雜的邏輯和架構

·•Reuse UI 元件

Page 43: AngularJS training in Luster

ng-app、ng-click…

就是 Angular 提供給我們的 Directives

DirectivesAngular built-in

Page 44: AngularJS training in Luster

Directives客製化

No more time, 我們跳過...

Page 45: AngularJS training in Luster

但是,還記得 Directive 幫我們解決什麼嗎?

Page 46: AngularJS training in Luster

Scopes

1. View 跟 Controller 利用它來做

View 和 Controller 都能夠存取 $scope,所以

溝通

Page 47: AngularJS training in Luster

View 跟 Controller 利用 Scope 做溝通

Page 48: AngularJS training in Luster

2. $scope 是一個簡單的 Data Model

儘管 Angular 加了一些 function 在裡面 但別想的太難

Page 49: AngularJS training in Luster

3. Scope 之間的關係就像

一個 App 裡有很多的 scope,

Page 50: AngularJS training in Luster

$rootScope.money = ‘1億’;

<body ng-app>

{{ money }} => 1億

Page 51: AngularJS training in Luster

<body ng-app>

<div ng-controller=“UncleCtrl”>

<div ng-controller=“FatherCtrl”>

{{ money }} => 1億

$scope.money = ‘2億’;{{ money }} => 2億

{{ money }} => 1億

$rootScope.money = ‘1億’;

Page 52: AngularJS training in Luster

<body ng-app>

<div ng-controller=“UncleCtrl”>

<div ng-controller=“FatherCtrl”>

{{ money }} => 1億

$scope.money = ‘2億’;{{ money }} => 2億

{{ money }} => 1億

$rootScope.money = ‘1億’;

$scope.wife = ‘林依晨’;{{ wife }} => 林依晨

$scope.wife = ‘桂綸鎂’;{{ wife }} => 桂綸鎂

Page 53: AngularJS training in Luster

3. Scope 之間的關係就像

爺爺

⼤大伯 ⼆二伯 ⽗父親

堂哥 我 ⼩小弟

親屬關係

可繼承父親屬性

兄弟之間的屬性不會影響

Page 54: AngularJS training in Luster

Controllers

定義和初始某一個 View 上的功能和資料

並將它們綁定在 $scope 上

Page 55: AngularJS training in Luster

它們是生命共同體

還記得這張圖嗎?

Page 56: AngularJS training in Luster

Keywords in Round 1

Directives

• ng-app

• ng-controller

• ng-model

• ng-repeat

• ng-click

Page 57: AngularJS training in Luster

Keywords in Round 1

Filters

• filter

• orderBy

Page 58: AngularJS training in Luster

Keywords in Round 1

Services

• $scope

Page 59: AngularJS training in Luster

Concepts in Round 1

• Directives

• Scopes

• Controllers

Page 60: AngularJS training in Luster

接下來,

我們要做什麼呢?

Page 61: AngularJS training in Luster

Let’s Coding for 我愛正妹

Round 2

Page 62: AngularJS training in Luster

Modules

setter

angular.module('moduleName', []);

getter

angular.module('moduleName');

依賴的模組 !

Ex: [“module1”, “module2”]

Page 63: AngularJS training in Luster

• module.controller(name, function)

• module.service(name, function)

• module.directive(name, function)

• module.filter(name, function)

• …

angular.module API

Page 64: AngularJS training in Luster

Filter

Service

Directive

Controller

…etc

Module

Module 讓我們方便地打包這些東西

Page 65: AngularJS training in Luster

Filter

Service

Directive

Controller

…etc

Module

ModuleA

ModuleB

ModuleC

Module 讓程式可以被 Reuse

Page 66: AngularJS training in Luster

Filter

Service

Directive

Controller

…etc

Module

ModuleA

ModuleB

ModuleC

Module 會幫我們處理複雜的依賴關係

Page 67: AngularJS training in Luster

Services一個 singleton 物件,Controller 們可以共享它

Page 68: AngularJS training in Luster

可以想像成...

Page 69: AngularJS training in Luster

Mrs. Controller

Page 70: AngularJS training in Luster

Mrs. Controller

app.controller(‘fatHottyCtrl’, function ($scope, $http) { $scope.findAllHotties = function () { $http.get(…).then(…); }; $scope.findOneHotty = …; $scope.createHotty = …; $scope.updateHotty = …; $scope.deleteHotty = …; } );

Page 71: AngularJS training in Luster

Mrs. Controllerapp.controller(‘fatHottyCtrl’, function ($scope, $http) { $scope.findAllHotties = function () { $http.get(…).then(…); }; $scope.findOneHotty = …; $scope.createHotty = …; $scope.updateHotty = …; $scope.deleteHotty = …; } );

Mrs. Controller 2app.controller(‘fatHottyCtrl2’, function ($scope, $http) { $scope.findAllHotties = function () { $http.get(…).then(…); }; $scope.findOneHotty = …; $scope.createHotty = …; $scope.updateHotty = …; $scope.deleteHotty = …; } );

Page 72: AngularJS training in Luster

當你的 App 變得越來越大...

Page 73: AngularJS training in Luster

有東西重複了=工程師的禁忌

所以...

Page 74: AngularJS training in Luster

app.controller(‘fatHottyCtrl’, function ($scope, $http) { $scope.findAllHotties = function () { $http.get(…).then(…); }; $scope.findOneHotty = …; $scope.createHotty = …; $scope.updateHotty = …; $scope.deleteHotty = …; } );app.controller(‘fatHottyCtrl2’, function ($scope, $http) { $scope.findAllHotties = function () { $http.get(…).then(…); }; $scope.findOneHotty = …; $scope.createHotty = …; $scope.updateHotty = …; $scope.deleteHotty = …; }

app.service(‘hottyService’, function ($http) { this.findAllHotties = function () { $http… }; this.findOneHotty = …; this.createHotty = …; this.updateHotty = …; this.deleteHotty = …; } );

Page 75: AngularJS training in Luster

app.controller(‘thinHottyCtrl2’, function ($scope, hottyService) { $scope.hottyService = hottyService; } );

app.controller(‘thinHottyCtrl’, function ($scope, hottyService) { $scope.hottyService = hottyService; } );

app.service(‘hottyService’, function ($http) { this.findAllHotties = function () { $http… }; this.findOneHotty = …; this.createHotty = …; this.updateHotty = …; this.deleteHotty = …; } );

Page 76: AngularJS training in Luster

Services

Mrs. Controller Mrs. Controller 2

2. Singleton

1. 邏輯可以共同使用

3. Controller 更瘦了

Page 77: AngularJS training in Luster

·• Angular built-in service 像 $http、$location 等

·• Angular 提供我們很多方法創建自己的 Service

Services 備註

module.service()

module.provider()module.factory() = 不同方法,但都是為了創建 Service

Page 78: AngularJS training in Luster

Dependency Injection你要什麼,跟它講,然後,它就給你什麼

Page 79: AngularJS training in Luster

app.controller(‘thinHottyCtrl’,

function ($scope, hottyService) {

$scope.hottyService = hottyService;

}

);

Angular 會幫你管理物件的生命週期 你只需要很明確的跟它要

名稱必須正確

Page 80: AngularJS training in Luster

angular.module(‘a’, [‘b’])

.service(‘aService’, function (bService) {}

);

找人,總是要那個人曾經活過

確保它是存在的

angular.module(‘b’, [])

.service(‘bService’, function () {}

);

所以,如果你要的東西定義在其他模組,

請記得要 import 那個模組

Page 81: AngularJS training in Luster

app.controller(‘thinHottyCtrl’,

function ($scope, hottyService) {…}

);app.controller(‘thinHottyCtrl’,

function (hottyService, $scope) {…});

=排序並不重要

Page 82: AngularJS training in Luster

app.service(‘aService’,

function (bService) {…});

app.service(‘bService’,

function (aService) {…});

無法互相依賴

Page 83: AngularJS training in Luster

DI 的好處

app.controller(‘thinHottyCtrl1’,

function ($scope, hottyService) {…});

app.controller(‘thinHottyCtrl2’,

function ($scope, hottyService) {…});

function HottyServiceA() {…};

app.service(‘hottyService’, HottyServiceA);

Page 84: AngularJS training in Luster

鬆耦合

app.controller(‘thinHottyCtrl1’,

function ($scope, hottyService) {…});

app.service(‘hottyService’, HottyServiceB);

function HottyServiceA() {…};

app.controller(‘thinHottyCtrl2’,

function ($scope, hottyService) {…});

function HottyServiceB() {…};

Page 85: AngularJS training in Luster

Keywords in Round 2

Services

• $http

Page 86: AngularJS training in Luster

Concepts in Round 2

• Modules

• Services

• Dependency Injection

Page 87: AngularJS training in Luster

Round 3

接下來,

交給你盡情發揮...

Page 88: AngularJS training in Luster

https://github.com/shiningjason1989/ng-tutorial

You can get sources on

Page 89: AngularJS training in Luster

Now, you know How to use it

Scopes

Controllers

Services

Directives

Dependency Injection

Page 90: AngularJS training in Luster

Finally, What is it

HTML enhanced

for web apps

Page 91: AngularJS training in Luster

謝謝您的沒有睡著你有嗎?如果你錯過了什麼,或者熱血的想找人討論,

歡迎您找我:)

By Jason Chung - [email protected]

達暉資訊