58
AngularJS入門篇 開發技巧實戰系列(1/6) - Web 前端技術 講師: 郭二文

01 startoff angularjs

Embed Size (px)

Citation preview

Page 1: 01 startoff angularjs

AngularJS:入門篇開發技巧實戰系列(1/6) - Web 前端技術講師: 郭二文

Page 2: 01 startoff angularjs

Document, Source code & Training Video• https://github.com/erhwenkuo/PracticalCoding

Page 3: 01 startoff angularjs

References

• The training material of this session are base on Dan Wahlin’s terrific “AngularJS Fundamentals in 60-ish Minutes”

• https://www.youtube.com/watch?v=i9MHigUZKEM

Page 4: 01 startoff angularjs

Agenda

• AngularJS Introduction

• Getting Started & Features

• Directives, Filters and Data Binding

• Views, Controllers and Scope

• Modules, Routes and Factories

Page 5: 01 startoff angularjs

AngularJS Introduction

Page 6: 01 startoff angularjs

AngularJS Website

Http://angularjs.org

Page 7: 01 startoff angularjs

Feeling About AngularJS Over Time

1. Neat

What? This is so lame!

2. Ok, that’s pretty sweet!

I can’t believe how difficult they make some of this stuff! Are you kidding me?!

3. Very Cool!

We’ve made a horrible choice! We should went with Backbone.

4. OMG, that’s awesome!

Better freshen up the old resume.

5. Most awesome framework ever!

Page 8: 01 startoff angularjs

Feeling About AngularJS Over Time

Page 9: 01 startoff angularjs

Feeling About AngularJS Over Time

Page 10: 01 startoff angularjs

Getting Started & Features

Page 11: 01 startoff angularjs

Single Page Application (SPA)

Page 12: 01 startoff angularjs

The Challenge with SPAs

DOM Manipulation

Routing

Data Binding

Caching

History Module Loading

Object Modeling

Ajax/Promises

View Loading

Page 13: 01 startoff angularjs

AngularJS is a full-featured SPA framework

Data Binding MVC Routing Testing

jqLite Templates History Factories

ViewModel Controllers Views Directives

Services Dependency Injection Validation

Page 14: 01 startoff angularjs

Developing Environment & Source Tree

Configure the Machine

• .NET Framework 4.51

• Visual Studio 2013 (Update 4)

Source Tree Folder Structure

• doc : Contains documents related to the code base

• lib : Contains all third-party and packages used by the application, including NuGet downloaded packages

• src : Contains all the source code, including Visual Studio file(s) and all project folders

PracticalCoding

doc

lib

src

Page 15: 01 startoff angularjs

Developing Environment & Source Tree

Creating The Solution Folder Structure

• Create a “Blank Solution” named “PracticalCoding” in Visual Studio

• Close Visual Studio and copy “PracticalCoding.sln” to “src” folder

• Delete the empty folder

• Use text editor to create “nugget.config” file with below content under “src” folder

PracticalCoding

doc

lib

src

PracticalCoding.sln

Nugget.config

Page 16: 01 startoff angularjs

Developing Environment & Source Tree

• Use File explorer to open “PracticalCoding.sln” file

• Add new “ASP.NET Web Application” project under “PracticalCoding” solution in Visual Studio

• Name this web project to “PracticalCoding.Web”

• Click “OK”

Page 17: 01 startoff angularjs

Developing Environment & Source Tree

• Choose “Web API” template

• Click “Change Authentication” to “No Authentication”

• Click “OK”

Page 18: 01 startoff angularjs

Developing Environment & Source Tree

Setup AngularJS

1. Download most update AngularJSfrom https://angularjs.org

2. Unzip and rename to “angular”

3. Copy and paste to C# project under “/Scripts” folder

4. Include the whole “/Scripts/angular” folder into C# project

Page 19: 01 startoff angularjs

Directives, Filters and Data Binding

Page 20: 01 startoff angularjs

What are Directives?

They teach HTML new tricks!

Page 21: 01 startoff angularjs

Using Directives and Data Binding Syntax

Page 22: 01 startoff angularjs

Using Directives and Data Binding Syntax (step_01_blankpage.html)

Demo Page

1. Create “MyApp” folder

2. Create “01_DirectivesAndDataBinding” folder under “MyApp”

3. Add new html file named “step_01_blankpage.html”

4. Drag “angular.js” from “Scripts” folder inside”<body></body>”

Page 23: 01 startoff angularjs

Using Directives and Data Binding Syntax (step_02_normalpage.html)

1. Copy “step_01_blankpage.html” as “step_02_normalpage.html

2. Add a simple html “input” tag

3. Hit “F5” and see the result

Demo Page

Page 24: 01 startoff angularjs

Using Directives and Data Binding Syntax (step_03_adddirectivedatabind.html)

1. Copy “step_02_normalpage.html” as “step_03_adddirectivedatabind.html ”

2. Add “ng-app” as attribute within “<html>” tag

3. Add “ng-model=‘vm.name’” within “<input>” tag

4. Add “{{vm.name}}” after “<input>” tage

5. Hit “F5” and see the result

Demo Page

Page 25: 01 startoff angularjs

Iterating with the ng-repeat Directive

1. “ng-init” is the angular directive to initialize some data

2. “ng-init” is the angular directive to iterate collection of data

Page 26: 01 startoff angularjs

Iterating with the ng-repeat Directive(step_04_iteratewithngrepeat)

1. Copy “step_03_adddirectivedatabind.html” as “step_04_iteratewithngrepeat.html ”

2. Add “ng-init=….” as attribute in <body> tag and add “ng-repeat” as attribute in <li> tag.

3. Add angular expression “{{personName}} ” to show binding value

4. Hit “F5” and see the result

Demo Page

Page 27: 01 startoff angularjs

AngularJS Directives Document

Page 28: 01 startoff angularjs

Using Filters

Filter in AngularJS is not only used to filter data, but also it can be used to format data for display as well

Page 29: 01 startoff angularjs

Iterating with the ng-repeat Directive(step_05_nginitcustomers)

1. Copy “step_04_iteratewithngrepeat.html” as “step_05_nginitcustomers.html”

2. Change “ng-init=…. “ to initialize a array of Customer objects

3. Change “ng-repeat” to iterate this new customers collection. And Change angular expression “{{customer.name}} -“{{customer.city}} ” to show binding value

4. Hit “F5” and see the result

Demo Page

Page 30: 01 startoff angularjs

Iterating with the ng-repeat Directive(step_06_filterbyinputandformat)

1. Copy “step_05_nginitcustomers.html” as “step_06_filterbyinputandformat.html”

Demo Page

Page 31: 01 startoff angularjs

AngularJS Filter Component List

Page 32: 01 startoff angularjs

Views, Controllers and Scope

Page 33: 01 startoff angularjs

AngularJS View, Controllers & Scope

View Controller$scope

1. $scope is the “glue” (ViewModel) between a controller and a view

2. The view don’t have to know about the controller

3. The controller definitely doesn’t want to know about the view

Page 34: 01 startoff angularjs

AngularJS View, Controllers & $scope(step_07_simplecontroller)

Copy “step_06_filterbyinputandformat.html” as “step_07_simplecontroller.html”

Demo Page

$scope is injected

Assign view to specific controller

ng-repeat access the vm.customers via

$scope

Page 35: 01 startoff angularjs

AngularJS View, Controllers & $scope

Page 36: 01 startoff angularjs

Modules, Routes and Factories

Page 37: 01 startoff angularjs

AngularJS Architecture (Simplified)

View Controller$scope

Routes

Config

Module

Directives Factory

Page 38: 01 startoff angularjs

Modules are Containers

Controller

Routes

Config

Module

Directives FactoryFilterService

ProviderValue

Page 39: 01 startoff angularjs

Creating AngularJS Module & Controller

Page 40: 01 startoff angularjs

AngularJS Module and Controllers (step_07_simplecontroller)

step_07_simplecontroller.html

Demo Page

$scope is injected

Assign view to specific controller

Page 41: 01 startoff angularjs

The Role of Routes

Page 42: 01 startoff angularjs

Defining Routes

Page 43: 01 startoff angularjs

AngularJS Define Routes Demo (step_08_defineroutes)

Copy “step_07_simplecontroller.html” as “step_08_defineroutes.html”

Demo Page

Include “angular-route.js”

Using “$routeProvider” to

define routes

Add one <div> with “ng-view” inside

Page 44: 01 startoff angularjs

AngularJS Define Routes Demo (step_08_defineroutes)

Create “View1.html” under folder “Partials_step_08”

Demo Page

Page 45: 01 startoff angularjs

AngularJS Define Routes Demo (step_08_defineroutes)

Create “View2.html” under folder “Partials_step_08”

Demo Page

Page 46: 01 startoff angularjs

AngularJS Define Routes Demo (step_08_defineroutes)

Select “step_08_defineroutes.html” and Hit “F5” to run

Demo Page

Page 47: 01 startoff angularjs

AngularJS Define Routes Demo (step_09_addcustomer)

Copy “step_08_defineroutes.html” as “step_09_addcustomer.html”

Demo Page

Create a new variable for

newCustomer

Add a new method “addCustomer()”

under “SimpleController”

Create a new variable for

newCustomerDefine routings

Page 48: 01 startoff angularjs

AngularJS Define Routes Demo (step_09_addcustomer)

Copy“Partials_step_08” to “Partials_step_09” & Modify “View1.html”

Demo Page

Binding input to newCustomer

Use “ng-click” to call “addCustomer()” method

Page 49: 01 startoff angularjs

AngularJS Define Routes Demo (step_09_addcustomer)

Select “step_09_addcustomer.html” and Hit “F5” to run

Demo Page

Page 50: 01 startoff angularjs

Using Factories and Services

Controller

Routes

Config

Module

Directives FactoryFilterService

ProviderValue

Page 51: 01 startoff angularjs

The Role of Factories

Page 52: 01 startoff angularjs

AngularJS Factory Demo (step_10_factorytogetcustomer)

1. Copy “step_09_addcustomer.html” as “step_10_factorytogetcustomer.html”

2. Add AngularJS factory “SimpleFactory”

Demo Page

Create a new factory “SimpleFactory“

Define a javascriptobject instance for

return

Create a new variable for

newCustomer

Define factory own variables and

methods for external access

Page 53: 01 startoff angularjs

AngularJS Factory Demo (step_10_factorytogetcustomer)Demo Page

Return javascriptobject instance

Create a new variable for

newCustomer

Define factory own variables and

methods for external access

Page 54: 01 startoff angularjs

AngularJS Factory Demo (step_10_factorytogetcustomer)

Use “SimpleFactory” to access shared

methods or attributes

Page 55: 01 startoff angularjs

AngularJS Factory Demo (step_10_factorytogetcustomer)

Config Routings for Create, Update &

Delete views

Page 56: 01 startoff angularjs

AngularJS Factory Demo (step_10_factorytogetcustomer)

Define Create/Update/Delete

View Tempaltes

Page 57: 01 startoff angularjs

AngularJS Factory Demo (step_10_factorytogetcustomer)

Select “step_10_factorytogetcustomer.html” and Hit “F5” to run

Demo Page

Page 58: 01 startoff angularjs

Next Session:AngularJS with Highchart