Top Banner
Introducing AngularJS
29
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introducing AngularJS

Introducing AngularJS

Page 2: Introducing AngularJS

Loc Nguyen● Java, Ruby, JavaScript, C#, PHP

● Sr Engineer at SignNow, full stack consultant

● AngularJS OC and Code for OC meetup

organizer

● @locn

[email protected]

Page 3: Introducing AngularJS

Agenda● The single page app● Live coding…

o Data bindingo Controllerso Serviceso Directives

● Examine an app….?

Page 4: Introducing AngularJS

Not on Agenda● REST integration● Promises● Routing● Testing● Angular UI● Yeoman, Bower, Grunt, Gulp● Integrating non-Angular libraries● Performance gotchas* egghead.io

Page 5: Introducing AngularJS

Thick client pains● Tight coupling to server technologies

● Model or DOM as the truth

● Promoting maintainability

○ Responding to change

○ Ease of testing

○ Organized codeOOP

}

Page 6: Introducing AngularJS

History for hipsters● DHTML● Web 2.0● Rich Internet Applications● Ambitious web apps™

Page 7: Introducing AngularJS

State of the MVC

Page 8: Introducing AngularJS

The Trend of Things

Page 9: Introducing AngularJS

(Rock) Stars

Page 10: Introducing AngularJS

Double Jeopardy?

Page 11: Introducing AngularJS

Web vs Desktop MVCModel2 MVC● Model notifies no one● Controller pulls

changes into view with each request

● Browser renders model state

MVC● Model notifies

observers● View observes

models● View reflect model

state

Page 12: Introducing AngularJS
Page 13: Introducing AngularJS

Data binding in Angular*● $scope

o application glueo source of truth, or a view modelo provides observers for state changes

Page 14: Introducing AngularJS

Data binding example(Angularians plz ignore anti-patterns)

Page 15: Introducing AngularJS

Data binding in Angular*● $watch list

// pseudo code, implicit watchers from example

$watchList = [$watch('scope.tweet'),$watch('scope.imageUrl')

]

Page 16: Introducing AngularJS

Data binding in Angular*● $watch function, explicit watcher

$scope.$watch(function () {

return map.currentCity;}, function (center) {

// update OKCupid match list});

Page 17: Introducing AngularJS

Explicit watcher example

Page 18: Introducing AngularJS

Data binding in Angular*● $digest loop

o Loop through the $watch list and do a dirty check

o Keep track of new valueso Keep looping until no values have changedo Finally, repaint DOM

● $digest triggerso ng-events, ng built-in services, $scope.

$apply()

Page 19: Introducing AngularJS

Dependency Injection● Reduce coupling● Improves testability● Promotes reusability

Page 20: Introducing AngularJS

Dependency Injectionfunction travel(a, z) {

setOrigin(a);setDest(z);var route =

getRoute();

var car = new Car();car.go(route);

}

travel('Irvine', 'Orange')

Page 21: Introducing AngularJS

Dependency Injection

function travel(a, z, transport) {

setOrigin(a);setDest(z);

var route = getRoute();transport.go(route);

}

travel(1985, 1955);

travel(1985, 1955, new TimeMachine());

Page 22: Introducing AngularJS

Controllers● More glue between view and model● Manage data models● Created and destroyed with route/view

changes

Page 23: Introducing AngularJS

Services● Singleton objects● Communication between controllers● Maintain state for lifetime of app

Page 24: Introducing AngularJS

Controller & Service Example

Page 25: Introducing AngularJS

DirectivesThe Awesome Sauce™ in AngularJS● Good on anything● Apply liberally● Carry extra

Page 26: Introducing AngularJS

Directives● Built-in directives

○ ng-show, ng-click, ng-repeat

● Custom directives○ reusable widgets○ declarative programming○ wrap non Angular libraries

Page 27: Introducing AngularJS

Directive Examples

Page 28: Introducing AngularJS

Mobile web app example

Page 29: Introducing AngularJS

meetup.com/angularjs-oc

meetup.com/CodeforOC