SlideShare ist ein Scribd-Unternehmen logo
1 von 48
Downloaden Sie, um offline zu lesen
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/

Weitere ähnliche Inhalte

Was ist angesagt?

Overview of the AngularJS framework
Overview of the AngularJS framework Overview of the AngularJS framework
Overview of the AngularJS framework Yakov Fain
 
AngularJS best-practices
AngularJS best-practicesAngularJS best-practices
AngularJS best-practicesHenry Tao
 
AngularJS - What is it & Why is it awesome ? (with demos)
AngularJS - What is it & Why is it awesome ? (with demos)AngularJS - What is it & Why is it awesome ? (with demos)
AngularJS - What is it & Why is it awesome ? (with demos)Gary Arora
 
AngularJs presentation
AngularJs presentation AngularJs presentation
AngularJs presentation Phan Tuan
 
Why angular js Framework
Why angular js Framework Why angular js Framework
Why angular js Framework Sakthi Bro
 
Angular JS - Introduction
Angular JS - IntroductionAngular JS - Introduction
Angular JS - IntroductionSagar Acharya
 
AngularJS - Architecture decisions in a large project 
AngularJS - Architecture decisionsin a large project AngularJS - Architecture decisionsin a large project 
AngularJS - Architecture decisions in a large project Elad Hirsch
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedStéphane Bégaudeau
 
AngularJS Basics
AngularJS BasicsAngularJS Basics
AngularJS BasicsRavi Mone
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJSWei Ru
 
AngularJS - The Next Big Thing?
AngularJS - The Next Big Thing?AngularJS - The Next Big Thing?
AngularJS - The Next Big Thing?Tom Hombergs
 
AngularJS for designers and developers
AngularJS for designers and developersAngularJS for designers and developers
AngularJS for designers and developersKai Koenig
 

Was ist angesagt? (20)

Overview of the AngularJS framework
Overview of the AngularJS framework Overview of the AngularJS framework
Overview of the AngularJS framework
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 
AngularJS best-practices
AngularJS best-practicesAngularJS best-practices
AngularJS best-practices
 
Introduction to Unit Tests and TDD
Introduction to Unit Tests and TDDIntroduction to Unit Tests and TDD
Introduction to Unit Tests and TDD
 
Angular js
Angular jsAngular js
Angular js
 
Angularjs
AngularjsAngularjs
Angularjs
 
Angular js
Angular jsAngular js
Angular js
 
AngularJS - What is it & Why is it awesome ? (with demos)
AngularJS - What is it & Why is it awesome ? (with demos)AngularJS - What is it & Why is it awesome ? (with demos)
AngularJS - What is it & Why is it awesome ? (with demos)
 
AngularJs presentation
AngularJs presentation AngularJs presentation
AngularJs presentation
 
Why angular js Framework
Why angular js Framework Why angular js Framework
Why angular js Framework
 
Angular JS - Introduction
Angular JS - IntroductionAngular JS - Introduction
Angular JS - Introduction
 
AngularJS Framework
AngularJS FrameworkAngularJS Framework
AngularJS Framework
 
AngularJS - Architecture decisions in a large project 
AngularJS - Architecture decisionsin a large project AngularJS - Architecture decisionsin a large project 
AngularJS - Architecture decisions in a large project 
 
AngularJS Basic Training
AngularJS Basic TrainingAngularJS Basic Training
AngularJS Basic Training
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get started
 
AngularJS Basics
AngularJS BasicsAngularJS Basics
AngularJS Basics
 
Angularjs Basics
Angularjs BasicsAngularjs Basics
Angularjs Basics
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
 
AngularJS - The Next Big Thing?
AngularJS - The Next Big Thing?AngularJS - The Next Big Thing?
AngularJS - The Next Big Thing?
 
AngularJS for designers and developers
AngularJS for designers and developersAngularJS for designers and developers
AngularJS for designers and developers
 

Andere mochten auch

Practices and obstacles in agile development
Practices and obstacles in agile developmentPractices and obstacles in agile development
Practices and obstacles in agile developmentGrgur Grisogono
 
High Performance Web Sites - 2008
High Performance Web Sites - 2008High Performance Web Sites - 2008
High Performance Web Sites - 2008Nate Koechley
 
Securing Client Side Data
 Securing Client Side Data Securing Client Side Data
Securing Client Side DataGrgur Grisogono
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJWORKS powered by Ordina
 
“Secure Password Managers” and “Military-Grade Encryption” on Smartphones:...
“Secure Password Managers” and   “Military-Grade Encryption” on  Smartphones:...“Secure Password Managers” and   “Military-Grade Encryption” on  Smartphones:...
“Secure Password Managers” and “Military-Grade Encryption” on Smartphones:...Positive Hack Days
 
Encryption vs tokenisation (for share)
Encryption vs tokenisation (for share)Encryption vs tokenisation (for share)
Encryption vs tokenisation (for share)AndrewRJamieson
 
Documenting your REST API with Swagger - JOIN 2014
Documenting your REST API with Swagger - JOIN 2014Documenting your REST API with Swagger - JOIN 2014
Documenting your REST API with Swagger - JOIN 2014JWORKS powered by Ordina
 
mwpc gas gain report
mwpc gas gain reportmwpc gas gain report
mwpc gas gain reportAndrew Schick
 
Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev
Microservices with Netflix OSS & Hypermedia APIs - JavaDay KievMicroservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev
Microservices with Netflix OSS & Hypermedia APIs - JavaDay KievJWORKS powered by Ordina
 
Big data document and graph d bs - couch-db and orientdb
Big data  document and graph d bs - couch-db and orientdbBig data  document and graph d bs - couch-db and orientdb
Big data document and graph d bs - couch-db and orientdbJWORKS powered by Ordina
 
Spring REST Docs: Documenting RESTful APIs using your tests - Devoxx
Spring REST Docs: Documenting RESTful APIs using your tests - DevoxxSpring REST Docs: Documenting RESTful APIs using your tests - Devoxx
Spring REST Docs: Documenting RESTful APIs using your tests - DevoxxJWORKS powered by Ordina
 

Andere mochten auch (20)

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

Ähnlich wie AngularJS Basics and Best Practices - CC FE &UX

AngularJS One Day Workshop
AngularJS One Day WorkshopAngularJS One Day Workshop
AngularJS One Day WorkshopShyam Seshadri
 
End to-End SPA Development Using ASP.NET and AngularJS
End to-End SPA Development Using ASP.NET and AngularJSEnd to-End SPA Development Using ASP.NET and AngularJS
End to-End SPA Development Using ASP.NET and AngularJSGil Fink
 
AngularJS: Overview & Key Features
AngularJS: Overview & Key FeaturesAngularJS: Overview & Key Features
AngularJS: Overview & Key FeaturesMohamad Al Asmar
 
Angularjs for kolkata drupal meetup
Angularjs for kolkata drupal meetupAngularjs for kolkata drupal meetup
Angularjs for kolkata drupal meetupGoutam Dey
 
Angularjs 131211063348-phpapp01
Angularjs 131211063348-phpapp01Angularjs 131211063348-phpapp01
Angularjs 131211063348-phpapp01Arunangsu Sahu
 
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013Mack Hardy
 

Ähnlich wie AngularJS Basics and Best Practices - CC FE &UX (20)

AngularJS One Day Workshop
AngularJS One Day WorkshopAngularJS One Day Workshop
AngularJS One Day Workshop
 
End to-End SPA Development Using ASP.NET and AngularJS
End to-End SPA Development Using ASP.NET and AngularJSEnd to-End SPA Development Using ASP.NET and AngularJS
End to-End SPA Development Using ASP.NET and AngularJS
 
AngularJS: Overview & Key Features
AngularJS: Overview & Key FeaturesAngularJS: Overview & Key Features
AngularJS: Overview & Key Features
 
Getting started with angular js
Getting started with angular jsGetting started with angular js
Getting started with angular js
 
AngularJS
AngularJSAngularJS
AngularJS
 
AngularJS
AngularJSAngularJS
AngularJS
 
Angularjs for kolkata drupal meetup
Angularjs for kolkata drupal meetupAngularjs for kolkata drupal meetup
Angularjs for kolkata drupal meetup
 
Getting started with angular js
Getting started with angular jsGetting started with angular js
Getting started with angular js
 
AngularJs
AngularJsAngularJs
AngularJs
 
Angularjs 131211063348-phpapp01
Angularjs 131211063348-phpapp01Angularjs 131211063348-phpapp01
Angularjs 131211063348-phpapp01
 
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
 
Asp 1a-aspnetmvc
Asp 1a-aspnetmvcAsp 1a-aspnetmvc
Asp 1a-aspnetmvc
 
Aspnetmvc 1
Aspnetmvc 1Aspnetmvc 1
Aspnetmvc 1
 
Angular js
Angular jsAngular js
Angular js
 
Introduction to AngularJs
Introduction to AngularJsIntroduction to AngularJs
Introduction to AngularJs
 
Angular js
Angular jsAngular js
Angular js
 
Angular js workshop
Angular js workshopAngular js workshop
Angular js workshop
 
Angular js
Angular jsAngular js
Angular js
 
AngularJS in practice
AngularJS in practiceAngularJS in practice
AngularJS in practice
 
Angular js
Angular jsAngular js
Angular js
 

Mehr von JWORKS powered by Ordina

Introduction to Webpack - Ordina JWorks - CC JS & Web
Introduction to Webpack - Ordina JWorks - CC JS & WebIntroduction to Webpack - Ordina JWorks - CC JS & Web
Introduction to Webpack - Ordina JWorks - CC JS & WebJWORKS powered by Ordina
 
Netflix OSS and HATEOAS deployed on production - JavaLand
Netflix OSS and HATEOAS deployed on production - JavaLandNetflix OSS and HATEOAS deployed on production - JavaLand
Netflix OSS and HATEOAS deployed on production - JavaLandJWORKS powered by Ordina
 
Cc internet of things LoRa and IoT - Innovation Enablers
Cc internet of things   LoRa and IoT - Innovation Enablers Cc internet of things   LoRa and IoT - Innovation Enablers
Cc internet of things LoRa and IoT - Innovation Enablers JWORKS powered by Ordina
 
Big data key-value and column stores redis - cassandra
Big data  key-value and column stores redis - cassandraBig data  key-value and column stores redis - cassandra
Big data key-value and column stores redis - cassandraJWORKS powered by Ordina
 
Android secure offline storage - CC Mobile
Android secure offline storage - CC MobileAndroid secure offline storage - CC Mobile
Android secure offline storage - CC MobileJWORKS powered by Ordina
 

Mehr von JWORKS powered by Ordina (19)

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

Kürzlich hochgeladen

Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac RoomVip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Roommeghakumariji156
 
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样ayvbos
 
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdfMatthew Sinclair
 
Best SEO Services Company in Dallas | Best SEO Agency Dallas
Best SEO Services Company in Dallas | Best SEO Agency DallasBest SEO Services Company in Dallas | Best SEO Agency Dallas
Best SEO Services Company in Dallas | Best SEO Agency DallasDigicorns Technologies
 
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查ydyuyu
 
Mira Road Housewife Call Girls 07506202331, Nalasopara Call Girls
Mira Road Housewife Call Girls 07506202331, Nalasopara Call GirlsMira Road Housewife Call Girls 07506202331, Nalasopara Call Girls
Mira Road Housewife Call Girls 07506202331, Nalasopara Call GirlsPriya Reddy
 
Call girls Service in Ajman 0505086370 Ajman call girls
Call girls Service in Ajman 0505086370 Ajman call girlsCall girls Service in Ajman 0505086370 Ajman call girls
Call girls Service in Ajman 0505086370 Ajman call girlsMonica Sydney
 
Local Call Girls in Seoni 9332606886 HOT & SEXY Models beautiful and charmin...
Local Call Girls in Seoni  9332606886 HOT & SEXY Models beautiful and charmin...Local Call Girls in Seoni  9332606886 HOT & SEXY Models beautiful and charmin...
Local Call Girls in Seoni 9332606886 HOT & SEXY Models beautiful and charmin...kumargunjan9515
 
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency""Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency"growthgrids
 
Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirtrahman018755
 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdfMatthew Sinclair
 
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查ydyuyu
 
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsIndian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsMonica Sydney
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC
 
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu DhabiAbu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu DhabiMonica Sydney
 
Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.krishnachandrapal52
 
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfpdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfJOHNBEBONYAP1
 
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrStory Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrHenryBriggs2
 
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime NagercoilNagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoilmeghakumariji156
 

Kürzlich hochgeladen (20)

Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac RoomVip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
 
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
 
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
 
Best SEO Services Company in Dallas | Best SEO Agency Dallas
Best SEO Services Company in Dallas | Best SEO Agency DallasBest SEO Services Company in Dallas | Best SEO Agency Dallas
Best SEO Services Company in Dallas | Best SEO Agency Dallas
 
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
 
Mira Road Housewife Call Girls 07506202331, Nalasopara Call Girls
Mira Road Housewife Call Girls 07506202331, Nalasopara Call GirlsMira Road Housewife Call Girls 07506202331, Nalasopara Call Girls
Mira Road Housewife Call Girls 07506202331, Nalasopara Call Girls
 
Call girls Service in Ajman 0505086370 Ajman call girls
Call girls Service in Ajman 0505086370 Ajman call girlsCall girls Service in Ajman 0505086370 Ajman call girls
Call girls Service in Ajman 0505086370 Ajman call girls
 
call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7
call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7
call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Local Call Girls in Seoni 9332606886 HOT & SEXY Models beautiful and charmin...
Local Call Girls in Seoni  9332606886 HOT & SEXY Models beautiful and charmin...Local Call Girls in Seoni  9332606886 HOT & SEXY Models beautiful and charmin...
Local Call Girls in Seoni 9332606886 HOT & SEXY Models beautiful and charmin...
 
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency""Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
 
Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirt
 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf
 
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
 
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsIndian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
 
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu DhabiAbu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
 
Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.
 
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfpdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
 
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrStory Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
 
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime NagercoilNagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
 

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/