web application development - github pages04 angularjs - part 2 19 to define an angularjs app, we...

43
Produced by Department of Computing & Mathematics Waterford Institute of Technology http://www.wit.ie Web Application Development David Drohan ([email protected] )

Upload: others

Post on 03-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Produced by

Department of Computing & Mathematics Waterford Institute of Technology

http://www.wit.ie

Web Application Development David Drohan ([email protected])

Page 2: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

MODULES, VIEWS, CONTROLLERS & ROUTES

PART 2

Page 3: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Sec8on Outline 1.  Introduc)on–WhyyoushouldbeusingAngularJS

2.  Terminology–Thecri:calfounda:onforunderstanding

3.  Modules–Reusablefunc:onality

4.  Views–UI(UserInterac:on)

5.  Controllers–Facilita:ngcommunica:onbetweenthemodelandtheview

6.  Routes–Naviga:ngtheview

7.  Filters–Changingthewayyouseethings

8.  Services–Fiverecipeflavors

9.  Direc)ves–ExtendingHTML

10.  CaseStudy–Labsinac:on11.  Conclusions–Theendisnigh

04ANGULARJS-PART2 3

Page 4: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Sec8on Outline 1.  Introduc)on–WhyyoushouldbeusingAngularJS

2.  Terminology–Thecri:calfounda:onforunderstanding

3.  Modules–Reusablefunc:onality4.  Views–UI(UserInterac:on)5.  Controllers–Facilita:ngcommunica:onbetweenthemodelandtheview

6.  Routes–Naviga:ngtheview7.  Filters–Changingthewayyouseethings

8.  Services–Fiverecipeflavors

9.  Direc)ves–ExtendingHTML

10.  CaseStudy–Labsinac:on11.  Conclusions–Theendisnigh

04ANGULARJS-PART2 4

Page 5: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Basic Building Blocks WHAT YOU NEED TO BUILD A BASIC ANGULAR WEB APP

04ANGULARJS-PART2 5

Page 6: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Basic Building Blocks * § InstallingAngularJSispreZysimple.Itisjustlikeaddinganyotherlibrary.

§ GototheAngularJS.orgwebsiteanddownloadthestableversionfromthere.

§ Youcanmanagethefiledirectoryas:

04ANGULARJS-PART2 6

Page 7: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Views WHAT THE USER INTERACTS WITH

04ANGULARJS-PART2 7

Page 8: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Basic Building Blocks – Views * § RecallthattheViewistheUserInterface,it’swhattheuserinteractswith§ Whenwri:nganAngularJSapp,wewritethebehaviorandinterac:ontogetheralongsidethepresenta:on(theView)

§ ViewsareocenreferredtoastemplatesinAngular

§ InSPAapps,arenderedtemplate(calledapar)al)isdynamicallyinsertedintoa‘shellpage’-Theshell’stemplateschangesdynamicallyover:me,henceSPA

§ Angularusesdirec)vestoachievethistemplateinser:on

§ Adirec)veisafancynameforafunc:onthat’saZachedtoaDOMelement.§ Direc:veshavetheabilitytoexecutemethods,definebehavior,aZachcontrollersand$scopeobjects,manipulatetheDOM,andmore.

04ANGULARJS-PART2 8

Page 9: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Basic Building Blocks – Views *  Direc:ves:ng-◦ Afewofthemostfrequentlyusedare:ü ng-app• DetermineswhichpartofthepagewilluseAngularJS•  Ifgivenavalueitwillloadthatapplica:onmodule

ü ng-controller• DetermineswhichJavaScriptControllershouldbeusedforthatpartofthepage

ü ng-model• Determineswhatmodelthevalueofaninputfieldwillbeboundto• Usedfortwo-waydatabinding(nextslide)

04ANGULARJS-PART2 9

Page 10: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

04ANGULARJS-PART2 10

2-Way Data Binding  Automa)cpropaga:onofdatachanges Modelissinglesourceoftruth

Page 11: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Two Way Data Binding Example *

04ANGULARJS-PART2 11

Page 12: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Two Way Data Binding Example

04ANGULARJS-PART2 12

Page 13: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Two Way Data Binding Example

04ANGULARJS-PART2 13

Page 14: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Basic Building Blocks – Views * l  More ng directives ü ng-if=“<model expression>” •  Inserts HTML element if expression is true •  Does not insert element in the DOM if it is false

ü ng-repeat=“<variable> in <array>” •  Repeats the HTML element for each value in the array

04ANGULARJS-PART2 14

Page 15: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Basic Building Blocks - Views

 AngularExpression:{{}}◦ Usedtoinsertmodelvaluesdirectlyintotheview◦  (anextractfromdona)ons.ejs)

04ANGULARJS-PART2 15

Page 16: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Modules REUSABLE FUNCTIONALITY

04ANGULARJS-PART2 16

Page 17: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Basic Building Blocks – Modules  Modulesareawayoforganizingyourcodeinwhichyousplituptheworkbetweendifferentsec:onsofyourcoderatherthanwri:ngasinglehugeapplica:on.

 Anapplica:onmodulecanincludetheothermodules(sec:ons)bylis:ngthemasdependencies.

04ANGULARJS-PART2 17

angular.module(<name>,[<dependencies>]);

Page 18: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Basic Building Blocks – Modules  Modulesareawayoforganizingyourcodeinwhichyousplituptheworkbetweendifferentsec:onsofyourcoderatherthanwri:ngasinglehugeapplica:on.

 Anapplica:onmodulecanincludetheothermodules(sec:ons)bylis:ngthemasdependencies.

04ANGULARJS-PART2 18

Page 19: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Module Defini8on *

 Defineamodule:

 Defineamodulewithdependenciesonothermodules:

 Getanexis:ngmodule:

04ANGULARJS-PART2 19

TodefineanAngularJSapp,wefirstneedtodefineanangular.module.AnAngularmoduleissimplyacollec:onoffunc:onsthatarerunwhentheapplica:onis“booted.”Allappshaveatleastonemodule.

Page 20: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

The Applica8on Module

HTMLFRAGMENT JAVASCRIPTFRAGMENT

AngularJSprovidesawayforyoutobindyourmainmoduletotheHTMLdocumentusingtheng-appdirec:ve.

04ANGULARJS-PART2 20

Page 21: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Module Phases CONFIG

 The config phase happens earlywhile theapplica:on is s:ll being built. Only theproviderservicesandconstantservicesarereadyforD.I.atthisstage.

RUN

 The runphasehappensonce themodulehasloadedallofitsservicesanddependencies.

var module = angular.module('myModule', []); module.config([function() { alert('I run first'); }]); module.run([function() { alert('I run second'); }]);

04ANGULARJS-PART2 21

Page 22: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Module Components & D.I.  AngularJSletsyouinjectservices(eitherfromitsownmoduleorfromothermodules)withthefollowingpaZern:

var module = angular.module('myModule', []); module.service('serviceA', function() { ... }); module.service('serviceB', function(serviceA) { ... });

04ANGULARJS-PART2 22

Page 23: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Dona8on MVC App using Modules *

04ANGULARJS-PART2 23

Page 24: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Dona8on MVC App using Modules

04ANGULARJS-PART2 24

Page 25: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Modules – App Design  Recommenda)ons:§ Amoduleforeachfeature.§ Amoduleforeachreusablecomponent

(especiallycustomdirec:vesandfilters)

§ Andanapplica:onlevelmodulewhichdependsontheabovemodulesandcontainsanyini:aliza:oncode.

04ANGULARJS-PART2 25

Page 26: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Controllers FACILITATING COMMUNICATION BETWEEN THE MODEL AND THE VIEW

04ANGULARJS-PART2 26

Page 27: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

04ANGULARJS-PART2 27

 Theinterfacebetweenthemodelandtheview Containsthecodebehindtheview Trytokeeplightweight

Basic Building Blocks - Controller

Page 28: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

04ANGULARJS-PART2 28

Basic Building Blocks - Scope §  A$scopeisanobjectthat:esaview(aDOMelement)tothecontroller

§  IntheModel-View-Controllerstructure,this$scopeobjectbecomesthemodel.

§  Itprovidesanexecu%oncontextthatisboundtotheDOMelement(anditschildren).

§  Althoughitsoundscomplex,the$scopeisjustaJavaScriptobject.§  Boththecontrollerandtheviewhaveaccesstothe$scopesoitcanbeused

forcommunica:onbetweenthetwo.§  This$scopeobjectwillhouseboththedataandthefunc:onsthatwe’llwant

torunintheview,aswe’llsee.

Page 29: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Basic Building Blocks - Scope  $scope◦ Containsdata(i.e.models)andmethods(i.e.func:ons)◦  Istheenginefor2-waydatabinding◦ Canaddyourownproper:es◦  $scope.<mynewproperty>=<value>;

 Controllerfunc:ontakesatleastoneparameter:$scope

04ANGULARJS-PART2 29

Page 30: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Controllers and Scope *

04ANGULARJS-PART2 30

 ‘Donate’Example

Page 31: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Controllers and Scope *

04ANGULARJS-PART2 31

 ‘Donate’Example

Page 32: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Controllers and Scope *

04ANGULARJS-PART2 32

 ‘Donate’Example

Page 33: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Controllers and Scope

04ANGULARJS-PART2 33

 ‘Donate’Example

Page 34: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Routes NAVIGATING THE VIEW

04ANGULARJS-PART2 34

Page 35: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Basic Building Blocks - Rou8ng AllowsSPAsbehaveliketradi:onalWebApps/sites§ forward/backbuZonsupport§ deep-linkingtospecificcontent§ Old-styleAJAXWebAppsdidn’tsupportrou:ng(addressabilityproblem)

§ Advantages§ Bookmarking,linksharing,directnaviga:on

§ TwoSolu:onApproaches:§ Hash-based,e.g.hZp://domain_name/#/some_app_url§ PushState,e.g.hZp://domain_name/some_app_urlAngularsupportsboth(nextslide)

04ANGULARJS-PART2 35

Page 36: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Basic Building Blocks - Rou8ng  PathsdefaulttoHash-basedmode◦  ExampleURL.◦  hZp://www.mysite.com/#/users

 CanuseHTML5modebyconfiguringthe$loca)onProvider(nextfewslides)◦  Ex.◦  //Inject$loca:onProviderintothemoduleusingconfig$loca:onProvider.html5Mode(true);

◦  ExampleURL:◦  hZp://www.mysite.com/users

04ANGULARJS-PART2 36

Page 37: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Basic Building Blocks - Rou8ng  UsedifferentviewsfordifferentURLfragments Makesuseoftemplatepar:als◦ Templatesthatarenotawholewebpage(i.e.partofapage)◦ Usedinconjunc:onwiththeng-viewdirec:ve◦ ng-viewdetermineswherethepar:alwillbeplaced◦ Canonlyhaveoneng-viewperpage

04ANGULARJS-PART2 37

Page 38: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Basic Building Blocks - Rou8ng  Enablebyinjec:ngthe$routeProvider◦  myApp=angular.module(‘myApp’,[‘ngRoute’]);myApp.config([‘$routeProvider’,func:on($routeProvider){…}]);

 $routeProvider.when(<path>,{<route>});◦  Definesanewroutethatusesthegivenpath◦  Thepathmayhaveparameters

◦  Parametersstartwithacolon(‘:’)◦  Ex-‘/user/:userId’ ($routeParams.userid)

◦  Typicalroutefields:◦  controller=Thenameofthecontrollerthatshouldbeused◦  templateUrl=Apathtothetemplatepar:althatshouldbeused

 $routeProvider.otherwise({<route>});◦  Typicalroutefields:

◦  redirectTo:‘<path>’

 API:hZp://docs.angularjs.org/api/ngRoute.$routeProvider04ANGULARJS-PART2 38

ModuleDependency

RouteObject

Page 39: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Rou8ng – The Shell Page  ng-viewdirec:ve–renderedtemplateofthecurrentrouteis

§ dynamicallyinsertedintotheshellpage(index.ejs)

§ URLchangeàTemplatechange+Controllerinstan:a:on

§ Shellpage=Layoutpage.

04ANGULARJS-PART2 39

index.ejs

direc:ve

Page 40: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Case Study LABS IN ACTION

04ANGULARJS-PART2 40

Page 41: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Demo Applica8on

04ANGULARJS-PART2 41

Page 42: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

About the Original Author  JamesSpeirs

 Applica:onFounda:ons

 OITCoreServices BrighamYoungUniversity

04ANGULARJS-PART2 42

Page 43: Web Application Development - GitHub Pages04 ANGULARJS - PART 2 19 To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collec:on

Questions?

04ANGULARJS-PART2 43