SlideShare a Scribd company logo
AngularJS Basics & Best Practices
Competence Center Front-end & UX
Dennis Jaamann & Frédéric Ghijselinck
Front-end devs
AngularJS Basics & Best Practices
1. What is AngularJS?
2. Why AngularJS?
3. Framework features
4. Framework components
5. Unit Testing With Karma
6. Best Practices
1. What is AngularJS?
▪ JavaScript MVVM framework
▪ Started in 2009 by Miško Hevery (Google)
▪ Declarative HTML
▪ Easy to create your own DSL
▪ Decoupling of DOM manipulation and application logic
2. Why AngularJS
▪ Becoming an industry standard for enterprise applications
▪ Large community
▪ Very active project
▪ Mature JavaScript framework
▪ Improved JavaScript development experience
▪ Structured JavaScript applications
▪ Separation of concerns
▪ Testability
▪ Modularity
▪ Maintainability
▪ Productivity
▪ Lots of tooling around AngularJS
▪ Documentation is not the best around
3. Framework features
▪ No
▪ Class (inheritance)
▪ Interfaces
▪ Type safety
▪ Place for storing data models
▪ Yes
▪ MVVM
▪ IOC container (DI)
▪ Two way data binding
▪ Testable javascript code
▪ Separation of concerns
3. Framework features - MVVM
▪ Angular is uses MVVM
▪ View
▪ HTML with Angular syntax
▪ ViewModel
▪ Known in Angular as $scope
▪ In view controllers
▪ In directives
▪ Model
▪ Services
3. Framework features - Dependency Injection
▪ Angular uses dependency injection
▪ Constructor injection by default
▪ Manual injection also possible
▪ Loose coupling of application components
▪ Controllers
▪ Services
▪ Directives
▪ ...
3. Framework features - 2-way data binding
▪ Classic frameworks use 1-way databinding
▪ Merge model and template to show view
▪ Updates to model not automatically reflected
▪ A lot of manual code needs to be written to update the view when the model changes
▪ More code === more bugs
3. Framework features - 2-way data binding
▪ AngularJS uses 2-way databinding
▪ Template compiled to view
▪ Updating
▪ On view update ⇒ Update model
▪ On model update ⇒ update view
▪ “Dirty checking” used to know what to update
▪ “Model” === $scope
▪ Automatic process
▪ Less code === less bugs
3. Framework features - Dirty checking
▪ Browser API’s suck currently (no Observables for DOM objects to know when something changes)
▪ Angular needs a manual loop to know
▪ Whether the DOM element changed and update the model
▪ Whether the model changed and update the view
▪ Multiple passes by default
▪ Only update when scope remains unchanged for x passes
▪ Maximum 10 passes
▪ Pitfall
▪ Dirty checking can slow down application substantially
▪ Potentially uses a lot of CPU cycles
▪ Keep objects/functions on your $scope as few as possible
▪ Less updates === less dirty checks
3. Framework features - Testable JavaScript code
▪ Angular core team wrote unit test runner called Karma
▪ Adapters for most common unit testing frameworks
▪ Jasmine (default, preffered)
▪ Mocha
▪ QUnit
▪ Integrates well with automation tools to setup CI
▪ Grunt
▪ Gulp
▪ Integration test library called Protractor
▪ NodeJS powered
▪ Based on WebDriverJS
▪ Mocks library to easily mock out dependencies
▪ angular-mocks
4. Framework components - Module
▪ Modules are containers for application components
▪ At least 1 module needed (main module)
▪ Can have submodules
▪ No out of the box lazy loading
▪ Plugins exist
4. Framework components - Module
▪ Example of a main module with submodules
4. Framework components - Scope
▪ Also known as view model in other frameworks
▪ Single source of truth
▪ Variables
▪ Functions
▪ Use in
▪ Controllers
▪ Directives
▪ Avoid in
▪ Services
▪ Sibling scopes cannot talk to each other ⇒ use events
▪ $broadcast / $emit
▪ $on / $watch
4. Framework components - Scope
▪ Inject $scope as a dependency
▪ $scope can contain variables and functions as members
Exercise 1 - Bootstrap your application
1. Switch to branch exercise1
2. In index.html
a. TODO 1 - Bootstrap your angular application and add controller 'ApplicationController'
b. TODO 2 - Load app.js
3. In app.js
a. TODO 3 - Create a controller 'ApplicationController' on the module, pass $scope and $log
b. TODO 4 - Log 'initializing application'
c. TODO 5 - Create a scope variable called 'hello', with content 'Hello Ordina'
4. In index.html
a. TODO 6 - Show the scope variable 'hello'
4. Framework components - View
▪ Also called templates or partials
▪ Regular HTML enhanced with
▪ Angular directives
▪ Custom directives
▪ Expressions {{ someExpression }}
4. Framework components - View
▪ Don’t
▪ Complex logic in expressions in view
▪ Refactor to scope function when possible
▪ Keep data-ng-controller directives out of your view
▪ Decouple view from controller (Reusability)
▪ Do
▪ Invoke scope functions
▪ Display scope variables
Exercise 2 - Leveraging modules and including views
1. Switch to branch exercise2
2. In index.html
a. TODO 1 - Remove the application controller
b. TODO 2 - Import scripts/common/module.js
c. TODO 3 - Import scripts/common/controllers/navigationController.js
3. In scripts/common/module.js
a. TODO 4 - Create and instantiate a new module called 'common'
4. In app.js
a. TODO 5 - Remove the controller
b. TODO 6 - Inject the common module
5. In index.html
a. TODO 7 - Create a new file views/common/navigation.html and include
Exercise 2 - Leveraging modules and including views
6. In scripts/common/controllers/navigationController.js
a. TODO 8 - Create new controller on common module, inject $scope and $window
b. TODO 9 - Create a new function onItemClick(item) on the scope, $window.alert the item
4. Framework components - Controller
▪ Don’t
▪ No DOM interactions ⇒ Encapsulate DOM interaction in directives
▪ No data manipulation ⇒ Encapsulate data manipulation in services
▪ Put everything on the scope ⇒ only what is needed (dirty checking performance)
▪ Share code/state between different controllers
▪ Manage lifecycle of other components
▪ Ajax requests ⇒ Encapsulate Ajax requests in services
▪ Do
▪ Scope variables
▪ View related logic
▪ Callbacks
▪ Handlers
▪ Broadcasts
▪ ...
4. Framework components - Controller
▪ Controller example
4. Framework components - Router
▪ Angular Module
▪ Configure which template/controller pair to use
▪ Keep data-ng-controller directive out of your HTML ⇒ use template with different controllers
▪ A route can be configured and consists of
▪ Template or TemplateUrl (required)
▪ Controller (required)
▪ Resolver (optional)
▪ Deeplinking out of the box
4. Framework components - Router
▪ Example of route configuration
Exercise 3 - Using routes and deeplinking
1. Switch to branch exercise3
2. In index.html
a. TODO 1 through 6 - Import the correct files
b. TODO 7 - Create a div with a data-ng-view directive to enable routing
3. In app.js
a. TODO 8 - Inject ngRoute module
b. TODO 9 - When default route, load tweetsOverview.html and use TweetsOverviewController
c. TODO 10 - When /about, load about.html and use AboutController
d. TODO 11 - When /contact, load contact.html and use ContactController
e. TODO 12 - Otherwise redirect to default route
4. In navigation.html
a. TODO 13 - Provide the correct data-ng-href for each link
4. Framework components - Service
▪ Don’t
▪ DOM manipulation
▪ Redirect to a route
▪ Manipulate lifecycle of other components
▪ Do
▪ Ajax requests
▪ Encapsulate complex business logic
▪ Data manipulation
▪ Filtering / Sorting
4. Framework components - Service
▪ An example of a service
Exercise 4 - Creating a service
1. Switch to branch exercise4
2. In index.html
a. TODO 1 through 4 - Import the correct files
3. In app.js
a. TODO 5 - Inject ngResource
b. TODO 6 - When /tweetdetail/:id, load tweetdetail.html and use TweetDetailController
4. In twitterSearchService.js
a. TODO 7 - Create a new resource with url /doSearch?q=:searchTerm&count=10
b. TODO 8 - Create a new function searchTweets(searchTerm)
c. TODO 9 - resource.get(searchTerm) and return a promise
5. In twitterOverviewController.js
a. TODO 10 - Call searchTweetsMethods on TwitterSearchService and handle promise
b. TODO 11 - Create a succeshandler for tweets and put result on $scope.tweets
4. Framework components - Resolver
▪ Part of a route
▪ Preload data from a service
▪ Inject data into controller
▪ Less code in controller
▪ Better testability
▪ Better separation of concerns
4. Framework components - Directive
▪ Extend HTML with custom tags
▪ Can have a dedicated template
▪ Can have a dedicated controller
▪ Do any DOM manipulation here, not in controller
▪ Has its own scope
▪ Can have its own controller
▪ Only use when you want to expose API for other directives
▪ Using data-ng-include? ⇒ Refactor to use a directive
4. Framework components - Directive
▪ Example of a directive
Exercise 5 - Using resolvers and directives
1. Switch to branch exercise5
2. In app.js
a. TODO 1 - Create a resolver fetching all tweets from the TwitterSearchService
b. TODO 2 - Create a resolver fetching tweet details by id from the TwitterSearchService (use $route.current.
params.id)
3. In tweetDetailController.js
a. TODO 3 - Inject tweet & put on scope
4. In tweetsOverviewController.js
a. TODO 4 - Inject tweets & put tweet.statuses on scope
5. In tweetDirective.js
a. TODO 5 - Create a directive that loads tweetDirective.html (use templateUrl)
6. In tweetsOverview.html
a. TODO 6 - Use the tweetDirective
Exercise 5 - Using resolvers and directives
7. In tweetDirective.html
a. TODO 7 - Show the created_at date in the directive
b. TODO 8 - Show the tweet.user.name
c. TODO 9 - Show the tweet.place.name and tweet.place.country
d. TODO 10 - Show the tweet.user.profile_image_url in an image tag
5. Unit Testing With Karma
▪ Karma = unit test runner (by angular core team)
▪ configuration file
▪ test suites
▪ headless/real browser
▪ Automagically generate a config file
▪ karma init
▪ Simply run the test suite
▪ karma start
5. Unit Testing - Jasmine
▪ Jasmine = unit testing framework
▪ test suite with specs:
5. Unit Testing - Jasmine
included matchers:
expect().toBe();
expect().toEqual();
expect().toMatch();
expect().toBeDefined();
expect().toBeUnDefined();
expect().toBeNull();
expect().toBeTruthy();
expect().toBeFalsy();
expect().toContain();
expect().toBeLessThan();
expect().toBeGreaterThan();
expect().toBeCloseTo();
expect().toThrow();
5. Unit Testing - Jasmine
▪ setup & teardown
▪ beforeEach()
▪ afterEach()
▪ beforeAll()
▪ afterAll()
▪ Angular test
▪ getting the module
▪ injecting the controller
5. Unit Testing - Controller
5. Unit Testing - Service
5. Unit Testing - Directive
6. Best practices
▪ Always prepend any directive with “data-”
▪ Valid HTML5
▪ Compatible in all browsers (IE sux :))
▪ Prefer attribute or element notation over comment and class notation
▪ Example
▪ <div data-my-custom-directive></div> (Preferred)
▪ <my-custom-directive></my-custom-directive>
6. Best practices
▪ Always use strict mode
▪ Enforces better programming practices
▪ Undefined global variables throw an error
▪ No usage of the with statement
▪ Create read-only properties
▪ Create non extensible objects
▪ ...
6. Best practices
▪ Wrap your components with a self executing anonymous function
▪ Keeps the global scope clean
▪ Better for minification
6. Best practices
▪ Single responsibility
▪ 1 file per component
▪ Directive
▪ Service
▪ Controller
▪ Filter
▪ App
▪ ..
6. Best practices
▪ Organize your files by feature
▪ 1 module per feature
6. Best practices
▪ Make your service methods return a promise
▪ You can use them in route resolvers
▪ Interpolated view expressions are smart enough to wait for the promise to resolve before rendering
▪ For example {{somePromiseOnTheScope}}
6. Best practices
▪ Create an application workflow
▪ Yeoman
▪ Scaffolding tool to quickly generate files and projects
▪ You can make your project-specific tools
▪ Grunt
▪ Build
▪ Test
▪ Concatenate
▪ Minify
▪ CDNify
▪ Bower
▪ Fetch external dependencies
▪ Integrate with CI server
Resources
▪ Books
▪ http://www.amazon.com/Mastering-Web-Application-Development-AngularJS/dp/1782161821
▪ http://www.amazon.com/Pro-AngularJS-Experts-Voice-Development/dp/1430264489/
▪ http://www.amazon.com/AngularJS-Brad-Green/dp/1449344852
▪ http://www.amazon.com/ng-book-Complete-AngularJS-Ari-Lerner/dp/099134460X/
▪ Other
▪ https://github.com/johnpapa/angularjs-styleguide
▪ http://www.adamcraven.me/a-better-module-structure-for-angular/
▪ https://github.com/daniellmb/angular-test-patterns
▪ http://trochette.github.io/Angular-Design-Patterns-Best-Practices/

More Related Content

PPTX
AngularJS Best Practices
PDF
AngularJS application architecture
PDF
Advanced Tips & Tricks for using Angular JS
PDF
AngularJS Best Practices
PDF
AngularJS introduction
PDF
Introduction to AngularJS
PDF
AngularJS Project Setup step-by- step guide - RapidValue Solutions
PPTX
The AngularJS way
AngularJS Best Practices
AngularJS application architecture
Advanced Tips & Tricks for using Angular JS
AngularJS Best Practices
AngularJS introduction
Introduction to AngularJS
AngularJS Project Setup step-by- step guide - RapidValue Solutions
The AngularJS way

What's hot (20)

PDF
Overview of the AngularJS framework
PPTX
Introduction to Angularjs
PDF
AngularJS best-practices
PDF
Introduction to Unit Tests and TDD
PPTX
Angular js
ODP
Angularjs
PPTX
Angular js
PDF
AngularJS - What is it & Why is it awesome ? (with demos)
PPTX
AngularJs presentation
PPTX
Why angular js Framework
PPTX
Angular JS - Introduction
PDF
AngularJS Framework
PPTX
AngularJS - Architecture decisions in a large project 
PDF
AngularJS Basic Training
PDF
AngularJS 101 - Everything you need to know to get started
PDF
AngularJS Basics
PPTX
Angularjs Basics
PPTX
Practical AngularJS
PPTX
AngularJS - The Next Big Thing?
KEY
AngularJS for designers and developers
Overview of the AngularJS framework
Introduction to Angularjs
AngularJS best-practices
Introduction to Unit Tests and TDD
Angular js
Angularjs
Angular js
AngularJS - What is it & Why is it awesome ? (with demos)
AngularJs presentation
Why angular js Framework
Angular JS - Introduction
AngularJS Framework
AngularJS - Architecture decisions in a large project 
AngularJS Basic Training
AngularJS 101 - Everything you need to know to get started
AngularJS Basics
Angularjs Basics
Practical AngularJS
AngularJS - The Next Big Thing?
AngularJS for designers and developers
Ad

Viewers also liked (20)

PDF
Practices and obstacles in agile development
PDF
High Performance Web Sites - 2008
PDF
Securing Client Side Data
PDF
JavaScript Basics and Best Practices - CC FE & UX
PDF
“Secure Password Managers” and “Military-Grade Encryption” on Smartphones:...
PPTX
Encryption vs tokenisation (for share)
PPTX
Cryptography
PDF
Batch Processing - A&BP CC
PDF
Documenting your REST API with Swagger - JOIN 2014
PDF
mwpc gas gain report
PDF
Responsive web - CC FE & UX
PPTX
Clean Code - A&BP CC
PPTX
Meteor - JOIN 2015
PPTX
PDF
Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev
PPTX
Big data document and graph d bs - couch-db and orientdb
PDF
Introduction to Docker
PDF
Spring REST Docs: Documenting RESTful APIs using your tests - Devoxx
PPTX
Mongo db intro.pptx
PDF
Unit testing - A&BP CC
Practices and obstacles in agile development
High Performance Web Sites - 2008
Securing Client Side Data
JavaScript Basics and Best Practices - CC FE & UX
“Secure Password Managers” and “Military-Grade Encryption” on Smartphones:...
Encryption vs tokenisation (for share)
Cryptography
Batch Processing - A&BP CC
Documenting your REST API with Swagger - JOIN 2014
mwpc gas gain report
Responsive web - CC FE & UX
Clean Code - A&BP CC
Meteor - JOIN 2015
Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev
Big data document and graph d bs - couch-db and orientdb
Introduction to Docker
Spring REST Docs: Documenting RESTful APIs using your tests - Devoxx
Mongo db intro.pptx
Unit testing - A&BP CC
Ad

Similar to AngularJS Basics and Best Practices - CC FE &UX (20)

PPTX
Angular js for Beginnners
PPTX
Angular workshop - Full Development Guide
PDF
AngularJS in Production (CTO Forum)
PDF
AngularJS Workshop
PDF
AngularJS Beginner Day One
PPTX
Angular js 1.0-fundamentals
PPTX
Angular Presentation
PPTX
AngularJS Introduction (Talk given on Aug 5 2013)
PDF
AngularJS - TechTalk 3/2/2014
PPTX
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
PPTX
Learning AngularJS - Complete coverage of AngularJS features and concepts
PPTX
AngularJS
PPTX
Understanding angular js
PDF
A gently introduction to AngularJS
PDF
Building Better Web Apps with Angular.js (SXSW 2014)
PPTX
Angular js
PPTX
AngularJS - a radically different way of building Single Page Apps
PPTX
Angular js 1.3 presentation for fed nov 2014
PDF
Leveling up with AngularJS
PPTX
AngularJS One Day Workshop
Angular js for Beginnners
Angular workshop - Full Development Guide
AngularJS in Production (CTO Forum)
AngularJS Workshop
AngularJS Beginner Day One
Angular js 1.0-fundamentals
Angular Presentation
AngularJS Introduction (Talk given on Aug 5 2013)
AngularJS - TechTalk 3/2/2014
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
Learning AngularJS - Complete coverage of AngularJS features and concepts
AngularJS
Understanding angular js
A gently introduction to AngularJS
Building Better Web Apps with Angular.js (SXSW 2014)
Angular js
AngularJS - a radically different way of building Single Page Apps
Angular js 1.3 presentation for fed nov 2014
Leveling up with AngularJS
AngularJS One Day Workshop

More from JWORKS powered by Ordina (19)

PDF
Introduction to Webpack - Ordina JWorks - CC JS & Web
PDF
Lagom in Practice
PDF
Netflix OSS and HATEOAS deployed on production - JavaLand
PDF
Cc internet of things @ Thomas More
PDF
An introduction to Cloud Foundry
PDF
Cc internet of things LoRa and IoT - Innovation Enablers
PPTX
Big data key-value and column stores redis - cassandra
PPTX
Hadoop bootcamp getting started
PPTX
Big data elasticsearch practical
PDF
Intro to cassandra
PPTX
Android wear - CC Mobile
PDF
Integration testing - A&BP CC
PDF
Spring 4 - A&BP CC
PPTX
Android secure offline storage - CC Mobile
PPTX
Java 7 & 8 - A&BP CC
PDF
IoT: A glance into the future
PDF
Workshop Ionic Framework - CC FE & UX
PDF
IoT: LoRa and Java on the PI
PDF
IoT: An introduction
Introduction to Webpack - Ordina JWorks - CC JS & Web
Lagom in Practice
Netflix OSS and HATEOAS deployed on production - JavaLand
Cc internet of things @ Thomas More
An introduction to Cloud Foundry
Cc internet of things LoRa and IoT - Innovation Enablers
Big data key-value and column stores redis - cassandra
Hadoop bootcamp getting started
Big data elasticsearch practical
Intro to cassandra
Android wear - CC Mobile
Integration testing - A&BP CC
Spring 4 - A&BP CC
Android secure offline storage - CC Mobile
Java 7 & 8 - A&BP CC
IoT: A glance into the future
Workshop Ionic Framework - CC FE & UX
IoT: LoRa and Java on the PI
IoT: An introduction

Recently uploaded (20)

PDF
WebRTC in SignalWire - troubleshooting media negotiation
PDF
Paper PDF World Game (s) Great Redesign.pdf
PDF
Decoding a Decade: 10 Years of Applied CTI Discipline
PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PDF
The Internet -By the Numbers, Sri Lanka Edition
PDF
“Google Algorithm Updates in 2025 Guide”
PPTX
ENCOR_Chapter_11 - ‌BGP implementation.pptx
PDF
Behind the Smile Unmasking Ken Childs and the Quiet Trail of Deceit Left in H...
PDF
Triggering QUIC, presented by Geoff Huston at IETF 123
PPTX
Digital Literacy And Online Safety on internet
PPTX
Introduction to Information and Communication Technology
PPTX
ppt for upby gurvinder singh padamload.pptx
PPTX
PPT_M4.3_WORKING WITH SLIDES APPLIED.pptx
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PPTX
durere- in cancer tu ttresjjnklj gfrrjnrs mhugyfrd
PPT
256065457-Anaesthesia-in-Liver-Disease-Patient.ppt
PPTX
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
PPTX
QR Codes Qr codecodecodecodecocodedecodecode
PDF
An introduction to the IFRS (ISSB) Stndards.pdf
WebRTC in SignalWire - troubleshooting media negotiation
Paper PDF World Game (s) Great Redesign.pdf
Decoding a Decade: 10 Years of Applied CTI Discipline
Slides PPTX World Game (s) Eco Economic Epochs.pptx
The Internet -By the Numbers, Sri Lanka Edition
“Google Algorithm Updates in 2025 Guide”
ENCOR_Chapter_11 - ‌BGP implementation.pptx
Behind the Smile Unmasking Ken Childs and the Quiet Trail of Deceit Left in H...
Triggering QUIC, presented by Geoff Huston at IETF 123
Digital Literacy And Online Safety on internet
Introduction to Information and Communication Technology
ppt for upby gurvinder singh padamload.pptx
PPT_M4.3_WORKING WITH SLIDES APPLIED.pptx
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
INTERNET------BASICS-------UPDATED PPT PRESENTATION
durere- in cancer tu ttresjjnklj gfrrjnrs mhugyfrd
256065457-Anaesthesia-in-Liver-Disease-Patient.ppt
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
QR Codes Qr codecodecodecodecocodedecodecode
An introduction to the IFRS (ISSB) Stndards.pdf

AngularJS Basics and Best Practices - CC FE &UX

  • 1. AngularJS Basics & Best Practices Competence Center Front-end & UX Dennis Jaamann & Frédéric Ghijselinck Front-end devs
  • 2. AngularJS Basics & Best Practices 1. What is AngularJS? 2. Why AngularJS? 3. Framework features 4. Framework components 5. Unit Testing With Karma 6. Best Practices
  • 3. 1. What is AngularJS? ▪ JavaScript MVVM framework ▪ Started in 2009 by Miško Hevery (Google) ▪ Declarative HTML ▪ Easy to create your own DSL ▪ Decoupling of DOM manipulation and application logic
  • 4. 2. Why AngularJS ▪ Becoming an industry standard for enterprise applications ▪ Large community ▪ Very active project ▪ Mature JavaScript framework ▪ Improved JavaScript development experience ▪ Structured JavaScript applications ▪ Separation of concerns ▪ Testability ▪ Modularity ▪ Maintainability ▪ Productivity ▪ Lots of tooling around AngularJS ▪ Documentation is not the best around
  • 5. 3. Framework features ▪ No ▪ Class (inheritance) ▪ Interfaces ▪ Type safety ▪ Place for storing data models ▪ Yes ▪ MVVM ▪ IOC container (DI) ▪ Two way data binding ▪ Testable javascript code ▪ Separation of concerns
  • 6. 3. Framework features - MVVM ▪ Angular is uses MVVM ▪ View ▪ HTML with Angular syntax ▪ ViewModel ▪ Known in Angular as $scope ▪ In view controllers ▪ In directives ▪ Model ▪ Services
  • 7. 3. Framework features - Dependency Injection ▪ Angular uses dependency injection ▪ Constructor injection by default ▪ Manual injection also possible ▪ Loose coupling of application components ▪ Controllers ▪ Services ▪ Directives ▪ ...
  • 8. 3. Framework features - 2-way data binding ▪ Classic frameworks use 1-way databinding ▪ Merge model and template to show view ▪ Updates to model not automatically reflected ▪ A lot of manual code needs to be written to update the view when the model changes ▪ More code === more bugs
  • 9. 3. Framework features - 2-way data binding ▪ AngularJS uses 2-way databinding ▪ Template compiled to view ▪ Updating ▪ On view update ⇒ Update model ▪ On model update ⇒ update view ▪ “Dirty checking” used to know what to update ▪ “Model” === $scope ▪ Automatic process ▪ Less code === less bugs
  • 10. 3. Framework features - Dirty checking ▪ Browser API’s suck currently (no Observables for DOM objects to know when something changes) ▪ Angular needs a manual loop to know ▪ Whether the DOM element changed and update the model ▪ Whether the model changed and update the view ▪ Multiple passes by default ▪ Only update when scope remains unchanged for x passes ▪ Maximum 10 passes ▪ Pitfall ▪ Dirty checking can slow down application substantially ▪ Potentially uses a lot of CPU cycles ▪ Keep objects/functions on your $scope as few as possible ▪ Less updates === less dirty checks
  • 11. 3. Framework features - Testable JavaScript code ▪ Angular core team wrote unit test runner called Karma ▪ Adapters for most common unit testing frameworks ▪ Jasmine (default, preffered) ▪ Mocha ▪ QUnit ▪ Integrates well with automation tools to setup CI ▪ Grunt ▪ Gulp ▪ Integration test library called Protractor ▪ NodeJS powered ▪ Based on WebDriverJS ▪ Mocks library to easily mock out dependencies ▪ angular-mocks
  • 12. 4. Framework components - Module ▪ Modules are containers for application components ▪ At least 1 module needed (main module) ▪ Can have submodules ▪ No out of the box lazy loading ▪ Plugins exist
  • 13. 4. Framework components - Module ▪ Example of a main module with submodules
  • 14. 4. Framework components - Scope ▪ Also known as view model in other frameworks ▪ Single source of truth ▪ Variables ▪ Functions ▪ Use in ▪ Controllers ▪ Directives ▪ Avoid in ▪ Services ▪ Sibling scopes cannot talk to each other ⇒ use events ▪ $broadcast / $emit ▪ $on / $watch
  • 15. 4. Framework components - Scope ▪ Inject $scope as a dependency ▪ $scope can contain variables and functions as members
  • 16. Exercise 1 - Bootstrap your application 1. Switch to branch exercise1 2. In index.html a. TODO 1 - Bootstrap your angular application and add controller 'ApplicationController' b. TODO 2 - Load app.js 3. In app.js a. TODO 3 - Create a controller 'ApplicationController' on the module, pass $scope and $log b. TODO 4 - Log 'initializing application' c. TODO 5 - Create a scope variable called 'hello', with content 'Hello Ordina' 4. In index.html a. TODO 6 - Show the scope variable 'hello'
  • 17. 4. Framework components - View ▪ Also called templates or partials ▪ Regular HTML enhanced with ▪ Angular directives ▪ Custom directives ▪ Expressions {{ someExpression }}
  • 18. 4. Framework components - View ▪ Don’t ▪ Complex logic in expressions in view ▪ Refactor to scope function when possible ▪ Keep data-ng-controller directives out of your view ▪ Decouple view from controller (Reusability) ▪ Do ▪ Invoke scope functions ▪ Display scope variables
  • 19. Exercise 2 - Leveraging modules and including views 1. Switch to branch exercise2 2. In index.html a. TODO 1 - Remove the application controller b. TODO 2 - Import scripts/common/module.js c. TODO 3 - Import scripts/common/controllers/navigationController.js 3. In scripts/common/module.js a. TODO 4 - Create and instantiate a new module called 'common' 4. In app.js a. TODO 5 - Remove the controller b. TODO 6 - Inject the common module 5. In index.html a. TODO 7 - Create a new file views/common/navigation.html and include
  • 20. Exercise 2 - Leveraging modules and including views 6. In scripts/common/controllers/navigationController.js a. TODO 8 - Create new controller on common module, inject $scope and $window b. TODO 9 - Create a new function onItemClick(item) on the scope, $window.alert the item
  • 21. 4. Framework components - Controller ▪ Don’t ▪ No DOM interactions ⇒ Encapsulate DOM interaction in directives ▪ No data manipulation ⇒ Encapsulate data manipulation in services ▪ Put everything on the scope ⇒ only what is needed (dirty checking performance) ▪ Share code/state between different controllers ▪ Manage lifecycle of other components ▪ Ajax requests ⇒ Encapsulate Ajax requests in services ▪ Do ▪ Scope variables ▪ View related logic ▪ Callbacks ▪ Handlers ▪ Broadcasts ▪ ...
  • 22. 4. Framework components - Controller ▪ Controller example
  • 23. 4. Framework components - Router ▪ Angular Module ▪ Configure which template/controller pair to use ▪ Keep data-ng-controller directive out of your HTML ⇒ use template with different controllers ▪ A route can be configured and consists of ▪ Template or TemplateUrl (required) ▪ Controller (required) ▪ Resolver (optional) ▪ Deeplinking out of the box
  • 24. 4. Framework components - Router ▪ Example of route configuration
  • 25. Exercise 3 - Using routes and deeplinking 1. Switch to branch exercise3 2. In index.html a. TODO 1 through 6 - Import the correct files b. TODO 7 - Create a div with a data-ng-view directive to enable routing 3. In app.js a. TODO 8 - Inject ngRoute module b. TODO 9 - When default route, load tweetsOverview.html and use TweetsOverviewController c. TODO 10 - When /about, load about.html and use AboutController d. TODO 11 - When /contact, load contact.html and use ContactController e. TODO 12 - Otherwise redirect to default route 4. In navigation.html a. TODO 13 - Provide the correct data-ng-href for each link
  • 26. 4. Framework components - Service ▪ Don’t ▪ DOM manipulation ▪ Redirect to a route ▪ Manipulate lifecycle of other components ▪ Do ▪ Ajax requests ▪ Encapsulate complex business logic ▪ Data manipulation ▪ Filtering / Sorting
  • 27. 4. Framework components - Service ▪ An example of a service
  • 28. Exercise 4 - Creating a service 1. Switch to branch exercise4 2. In index.html a. TODO 1 through 4 - Import the correct files 3. In app.js a. TODO 5 - Inject ngResource b. TODO 6 - When /tweetdetail/:id, load tweetdetail.html and use TweetDetailController 4. In twitterSearchService.js a. TODO 7 - Create a new resource with url /doSearch?q=:searchTerm&count=10 b. TODO 8 - Create a new function searchTweets(searchTerm) c. TODO 9 - resource.get(searchTerm) and return a promise 5. In twitterOverviewController.js a. TODO 10 - Call searchTweetsMethods on TwitterSearchService and handle promise b. TODO 11 - Create a succeshandler for tweets and put result on $scope.tweets
  • 29. 4. Framework components - Resolver ▪ Part of a route ▪ Preload data from a service ▪ Inject data into controller ▪ Less code in controller ▪ Better testability ▪ Better separation of concerns
  • 30. 4. Framework components - Directive ▪ Extend HTML with custom tags ▪ Can have a dedicated template ▪ Can have a dedicated controller ▪ Do any DOM manipulation here, not in controller ▪ Has its own scope ▪ Can have its own controller ▪ Only use when you want to expose API for other directives ▪ Using data-ng-include? ⇒ Refactor to use a directive
  • 31. 4. Framework components - Directive ▪ Example of a directive
  • 32. Exercise 5 - Using resolvers and directives 1. Switch to branch exercise5 2. In app.js a. TODO 1 - Create a resolver fetching all tweets from the TwitterSearchService b. TODO 2 - Create a resolver fetching tweet details by id from the TwitterSearchService (use $route.current. params.id) 3. In tweetDetailController.js a. TODO 3 - Inject tweet & put on scope 4. In tweetsOverviewController.js a. TODO 4 - Inject tweets & put tweet.statuses on scope 5. In tweetDirective.js a. TODO 5 - Create a directive that loads tweetDirective.html (use templateUrl) 6. In tweetsOverview.html a. TODO 6 - Use the tweetDirective
  • 33. Exercise 5 - Using resolvers and directives 7. In tweetDirective.html a. TODO 7 - Show the created_at date in the directive b. TODO 8 - Show the tweet.user.name c. TODO 9 - Show the tweet.place.name and tweet.place.country d. TODO 10 - Show the tweet.user.profile_image_url in an image tag
  • 34. 5. Unit Testing With Karma ▪ Karma = unit test runner (by angular core team) ▪ configuration file ▪ test suites ▪ headless/real browser ▪ Automagically generate a config file ▪ karma init ▪ Simply run the test suite ▪ karma start
  • 35. 5. Unit Testing - Jasmine ▪ Jasmine = unit testing framework ▪ test suite with specs:
  • 36. 5. Unit Testing - Jasmine included matchers: expect().toBe(); expect().toEqual(); expect().toMatch(); expect().toBeDefined(); expect().toBeUnDefined(); expect().toBeNull(); expect().toBeTruthy(); expect().toBeFalsy(); expect().toContain(); expect().toBeLessThan(); expect().toBeGreaterThan(); expect().toBeCloseTo(); expect().toThrow();
  • 37. 5. Unit Testing - Jasmine ▪ setup & teardown ▪ beforeEach() ▪ afterEach() ▪ beforeAll() ▪ afterAll() ▪ Angular test ▪ getting the module ▪ injecting the controller
  • 38. 5. Unit Testing - Controller
  • 39. 5. Unit Testing - Service
  • 40. 5. Unit Testing - Directive
  • 41. 6. Best practices ▪ Always prepend any directive with “data-” ▪ Valid HTML5 ▪ Compatible in all browsers (IE sux :)) ▪ Prefer attribute or element notation over comment and class notation ▪ Example ▪ <div data-my-custom-directive></div> (Preferred) ▪ <my-custom-directive></my-custom-directive>
  • 42. 6. Best practices ▪ Always use strict mode ▪ Enforces better programming practices ▪ Undefined global variables throw an error ▪ No usage of the with statement ▪ Create read-only properties ▪ Create non extensible objects ▪ ...
  • 43. 6. Best practices ▪ Wrap your components with a self executing anonymous function ▪ Keeps the global scope clean ▪ Better for minification
  • 44. 6. Best practices ▪ Single responsibility ▪ 1 file per component ▪ Directive ▪ Service ▪ Controller ▪ Filter ▪ App ▪ ..
  • 45. 6. Best practices ▪ Organize your files by feature ▪ 1 module per feature
  • 46. 6. Best practices ▪ Make your service methods return a promise ▪ You can use them in route resolvers ▪ Interpolated view expressions are smart enough to wait for the promise to resolve before rendering ▪ For example {{somePromiseOnTheScope}}
  • 47. 6. Best practices ▪ Create an application workflow ▪ Yeoman ▪ Scaffolding tool to quickly generate files and projects ▪ You can make your project-specific tools ▪ Grunt ▪ Build ▪ Test ▪ Concatenate ▪ Minify ▪ CDNify ▪ Bower ▪ Fetch external dependencies ▪ Integrate with CI server
  • 48. Resources ▪ Books ▪ http://www.amazon.com/Mastering-Web-Application-Development-AngularJS/dp/1782161821 ▪ http://www.amazon.com/Pro-AngularJS-Experts-Voice-Development/dp/1430264489/ ▪ http://www.amazon.com/AngularJS-Brad-Green/dp/1449344852 ▪ http://www.amazon.com/ng-book-Complete-AngularJS-Ari-Lerner/dp/099134460X/ ▪ Other ▪ https://github.com/johnpapa/angularjs-styleguide ▪ http://www.adamcraven.me/a-better-module-structure-for-angular/ ▪ https://github.com/daniellmb/angular-test-patterns ▪ http://trochette.github.io/Angular-Design-Patterns-Best-Practices/