SlideShare una empresa de Scribd logo
1 de 51
Descargar para leer sin conexión
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Introduction to SPAs with
AngularJS
@LaurentDuveau
AngularAcademy.ca
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
I have been creating web sites for
20 years…
1995
My JavaScript
won’t work in
Netscape!
2015
Still doing
JavaScript…
… but with
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Agenda
• SPA ?
• AngularJS ?
• Setup and first
project
• Modules
• Controllers
• Directives
• Filters
• Navigation with
Routing
• Services
• REST API
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
SPA ?
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Single Page Application
Not an app with a single page…
Different views loaded into a screen without reloading
everything from scratch
+ routing, history, two-way data binding, dependency
injection, …
SPA are client-centric application… so coded with
JavaScript
… lots of JavaScript… rely on a Framework!
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
AngularJS ?
100% JavaScript Framework
Created in October 2010 by developers inside Google
Final first version in June 2012
Open Source, MIT license
Compatible with IE 9+ and others modern browsers
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Wait… another Js framework ?
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Another Js framework, really !??
“there are more JavaScript frameworks
than JavaScript developers”
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Js frameworks…
Google Trends
http://www.google.com/trends/explore?hl=en#q=backbone.js,+angularjs,+ember.js,+knockout.js
&cmpt=q
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
SETUP AND FIRST SAMPLE!
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Setup
index.html
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
First sample
index.htmlbinding
directives
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
The ng-* attributes
Angular Directives
• Custom HTML elements (standard!)
• Ignored by browsers, processed by
Angular
• Manage events and DOM
interactions
• Can have its own controller and
template
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
MODULES
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Modules
• Angular code is written in modules
• Better decoupling, maintenance and
testability
• A module can use other modules
dependenciesmodule
name
AngularJS!
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Modules
index.html
app.js
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Modules
index.html
app.js
runs this module
when page load
dependencies
none, for now…
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
CONTROLLERS
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Controller
• Behavior of the application specified in
controllers with functions and variables
controller in
the module
good practice:
write your code in an Immediately
Invoked Function Expression (IIFE)
app.js
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Controller
index.html
directive controller name alias
controller
scope
binding
app.js
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Controller
index.html
directive controller name alias
controller
scope
binding
app.js
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
DEMO!
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
DIRECTIVES
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Directives
• ng-model directive
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Directives
• Add a button with ng-click
index.html
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Directives
• Add a button with ng-click
index.html
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Directives
• ng-disabled directive
app.js
index.html
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Directives
• ng-disabled directive
app.js
index.html
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Directives
• ng-hide directive
index.html
app.js
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Directives
products array
app.js
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Directives
• ng-repeat directive
index.html
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Directives
• ng-repeat directive
index.html
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Directives
• ng-class directive
index.html
styles.css
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Directives
• ng-class directive
index.html
styles.css
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
FILTERS
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Filters
index.html
formats for
currency, date, …
app.js
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Filters
index.html
formats for
currency, date, …
app.js
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Bidirectional data binding!
When ng-click changes…
…the expression {{store.displayLimit}} (and limitTo) is
automatically updated!
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
ROUTING
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Routing ?
• Split a page in views and navigate between them.
About Us
View
ng-view
(placeholder)
Index.html
My App
Contact Us
View
#/About
#/Contact
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Routing with ngRoute
• Import angular-route.js
• Inject ngRoute into the module
• Define the routes
var app = angular.module('store', ['store-products', 'ngRoute']);
app.config(["$routeProvider", function ($routeProvider) {
$routeProvider.when("/", {
controller: "StoreController",
controllerAs: "storeCtrl",
templateUrl: "/views/productsList.html"
})
.when("/product/:id", {
controller: "ProductController",
controllerAs: "productCtrl",
templateUrl: "/views/productView.html"
})
.otherwise({ redirectTo: "/" });
}]);
mysite.com/index.html#/product/1
mysite.com/index.html
everything else…
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Routing with ngRoute
• Get Route parameter in the controller
• Use ngView in your page
<body>
<div ng-view=""></div>
. . .
</body>
index.html
app.controller('ProductController', function ($routeParams,
dataService) {
dataService.getProductById($routeParams.id)
.then(function (product) {
. . .
products.js
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
SERVICES
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Services
• Shares functionality and data between
controllers
• Avoids code redundancy
• Implemented as singletons (single
instance)
• AngularJS comes with built-in services
($http, $route, $log, $q, $resource, etc.)
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
$resource and REST API
angular.module('store', ['ngResource'])
.factory('Product', function($resource) {
return $resource('/api/product/:id');
})
.controller('MainCtrl', function(Product) {
var vm = this;
// Get all products
vm.products = Product.query();
// Form data for creating a new product with ng-model
vm.productData = {};
vm.newProduct = function() {
var product = new Product(vm.productData);
product.$save();
}
});
also get(), remove(), delete()
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
© LDEX, 2015 JavaScript Open Day Montreal, November 2015
Angular Training: 3-day course in Canada!

Más contenido relacionado

La actualidad más candente

Towards Modularity in Live Visual Modeling: A case-study with OpenPonk and Ke...
Towards Modularity in Live Visual Modeling: A case-study with OpenPonk and Ke...Towards Modularity in Live Visual Modeling: A case-study with OpenPonk and Ke...
Towards Modularity in Live Visual Modeling: A case-study with OpenPonk and Ke...
ESUG
 

La actualidad más candente (20)

Angular vs. React
Angular vs. ReactAngular vs. React
Angular vs. React
 
Angular 4 Tutorial | What's New In Angular 4 | Angular Training | Edureka
Angular 4 Tutorial | What's New In Angular 4 | Angular Training | EdurekaAngular 4 Tutorial | What's New In Angular 4 | Angular Training | Edureka
Angular 4 Tutorial | What's New In Angular 4 | Angular Training | Edureka
 
Talk for DevFest 2021 - GDG Bénin
Talk for DevFest 2021 - GDG BéninTalk for DevFest 2021 - GDG Bénin
Talk for DevFest 2021 - GDG Bénin
 
Coders Workshop: API First Mobile Development Featuring Angular and Node
Coders Workshop: API First Mobile Development Featuring Angular and NodeCoders Workshop: API First Mobile Development Featuring Angular and Node
Coders Workshop: API First Mobile Development Featuring Angular and Node
 
Smoothing the Continuous Delivery Path - A Tale of Two Teams
Smoothing the Continuous Delivery Path - A Tale of Two TeamsSmoothing the Continuous Delivery Path - A Tale of Two Teams
Smoothing the Continuous Delivery Path - A Tale of Two Teams
 
Angular CLI : HelloWorld
Angular CLI : HelloWorldAngular CLI : HelloWorld
Angular CLI : HelloWorld
 
GraphQL Bangkok Meetup 6.0
GraphQL Bangkok Meetup 6.0GraphQL Bangkok Meetup 6.0
GraphQL Bangkok Meetup 6.0
 
What is Angular version 4?
What is Angular version 4?What is Angular version 4?
What is Angular version 4?
 
Angular 2 : le réveil de la force
Angular 2 : le réveil de la forceAngular 2 : le réveil de la force
Angular 2 : le réveil de la force
 
OSGi for outsiders - Milen Dyankov
OSGi for outsiders - Milen DyankovOSGi for outsiders - Milen Dyankov
OSGi for outsiders - Milen Dyankov
 
Towards Modularity in Live Visual Modeling: A case-study with OpenPonk and Ke...
Towards Modularity in Live Visual Modeling: A case-study with OpenPonk and Ke...Towards Modularity in Live Visual Modeling: A case-study with OpenPonk and Ke...
Towards Modularity in Live Visual Modeling: A case-study with OpenPonk and Ke...
 
Contributing to Apache Projects and Making Profits
Contributing to Apache Projects and Making ProfitsContributing to Apache Projects and Making Profits
Contributing to Apache Projects and Making Profits
 
Getting started with Angular CLI
Getting started with Angular CLIGetting started with Angular CLI
Getting started with Angular CLI
 
React vs angular (mobile first battle)
React vs angular (mobile first battle)React vs angular (mobile first battle)
React vs angular (mobile first battle)
 
Getting Started with Angular 2
Getting Started with Angular 2Getting Started with Angular 2
Getting Started with Angular 2
 
ReactJS.NET
ReactJS.NETReactJS.NET
ReactJS.NET
 
Introduction to angular 2
Introduction to angular 2Introduction to angular 2
Introduction to angular 2
 
Waltz-Controls presentation for Canadian Light Source
Waltz-Controls presentation for Canadian Light SourceWaltz-Controls presentation for Canadian Light Source
Waltz-Controls presentation for Canadian Light Source
 
Angular 2 and Wijmo 5 - GrapeCity Echo 2016 Tokyo
Angular 2 and Wijmo 5 - GrapeCity Echo 2016 TokyoAngular 2 and Wijmo 5 - GrapeCity Echo 2016 Tokyo
Angular 2 and Wijmo 5 - GrapeCity Echo 2016 Tokyo
 
Angular coding: from project management to web and mobile deploy
Angular coding: from project management to web and mobile deployAngular coding: from project management to web and mobile deploy
Angular coding: from project management to web and mobile deploy
 

Destacado

Destacado (7)

DevTeach Ottawa - Silverlight5 and HTML5
DevTeach Ottawa - Silverlight5 and HTML5DevTeach Ottawa - Silverlight5 and HTML5
DevTeach Ottawa - Silverlight5 and HTML5
 
Introduction to Angular for .NET Developers
Introduction to Angular for .NET DevelopersIntroduction to Angular for .NET Developers
Introduction to Angular for .NET Developers
 
Angular 2... so can I use it now??
Angular 2... so can I use it now??Angular 2... so can I use it now??
Angular 2... so can I use it now??
 
Maximizing ROI in eCommerce with Search
Maximizing ROI in eCommerce with SearchMaximizing ROI in eCommerce with Search
Maximizing ROI in eCommerce with Search
 
Introduction to Angular with TypeScript for .NET Developers
Introduction to Angular with TypeScript for .NET DevelopersIntroduction to Angular with TypeScript for .NET Developers
Introduction to Angular with TypeScript for .NET Developers
 
Visual Design with Data
Visual Design with DataVisual Design with Data
Visual Design with Data
 
Build Features, Not Apps
Build Features, Not AppsBuild Features, Not Apps
Build Features, Not Apps
 

Similar a Introduction to SPAs with AngularJS

Novidades do ASP.NET e do Visual Studio 2013
Novidades do ASP.NET e do Visual Studio 2013Novidades do ASP.NET e do Visual Studio 2013
Novidades do ASP.NET e do Visual Studio 2013
Jiéverson Maissiat
 

Similar a Introduction to SPAs with AngularJS (20)

Overview of the AngularJS framework
Overview of the AngularJS framework Overview of the AngularJS framework
Overview of the AngularJS framework
 
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
 
ASP .NET MVC Introduction & Guidelines
ASP .NET MVC Introduction & Guidelines  ASP .NET MVC Introduction & Guidelines
ASP .NET MVC Introduction & Guidelines
 
Angularjs
AngularjsAngularjs
Angularjs
 
AngularJs Basic Concept
AngularJs Basic ConceptAngularJs Basic Concept
AngularJs Basic Concept
 
Introduction to AngularJs
Introduction to AngularJsIntroduction to AngularJs
Introduction to AngularJs
 
Angular
AngularAngular
Angular
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
Building Cross Platform Mobile Apps
Building Cross Platform Mobile AppsBuilding Cross Platform Mobile Apps
Building Cross Platform Mobile Apps
 
Get satrted angular js day 2
Get satrted angular js day 2Get satrted angular js day 2
Get satrted angular js day 2
 
Getting started with angular js
Getting started with angular jsGetting started with angular js
Getting started with angular js
 
Angular patterns
Angular patternsAngular patterns
Angular patterns
 
Building share point apps with angularjs
Building share point apps with angularjsBuilding share point apps with angularjs
Building share point apps with angularjs
 
Introduction to Protractor
Introduction to ProtractorIntroduction to Protractor
Introduction to Protractor
 
Why choose Angular 6?
Why choose Angular 6?Why choose Angular 6?
Why choose Angular 6?
 
Angular, the New Angular JS
Angular, the New Angular JSAngular, the New Angular JS
Angular, the New Angular JS
 
AngularJS is awesome
AngularJS is awesomeAngularJS is awesome
AngularJS is awesome
 
Novidades do ASP.NET e do Visual Studio 2013
Novidades do ASP.NET e do Visual Studio 2013Novidades do ASP.NET e do Visual Studio 2013
Novidades do ASP.NET e do Visual Studio 2013
 
Angular js
Angular jsAngular js
Angular js
 
Valentine with Angular js - Introduction
Valentine with Angular js - IntroductionValentine with Angular js - Introduction
Valentine with Angular js - Introduction
 

Más de Laurent Duveau

Más de Laurent Duveau (20)

Shit happens… debugging an Angular app.
Shit happens… debugging an Angular app.Shit happens… debugging an Angular app.
Shit happens… debugging an Angular app.
 
8 things you didn't know about the Angular Router, you won't believe #6!
8 things you didn't know about the Angular Router, you won't believe #6!8 things you didn't know about the Angular Router, you won't believe #6!
8 things you didn't know about the Angular Router, you won't believe #6!
 
De 0 à Angular en 1h30! (french)
De 0 à Angular en 1h30! (french)De 0 à Angular en 1h30! (french)
De 0 à Angular en 1h30! (french)
 
Angular 6, CLI 6, Material 6 (french)
Angular 6, CLI 6, Material 6 (french)Angular 6, CLI 6, Material 6 (french)
Angular 6, CLI 6, Material 6 (french)
 
Debugging an Angular App
Debugging an Angular AppDebugging an Angular App
Debugging an Angular App
 
TypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret WeaponTypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret Weapon
 
Introduction to Angular for .NET Developers
Introduction to Angular for .NET DevelopersIntroduction to Angular for .NET Developers
Introduction to Angular for .NET Developers
 
TypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret WeaponTypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret Weapon
 
Introduction to Angular for .NET Developers
Introduction to Angular for .NET DevelopersIntroduction to Angular for .NET Developers
Introduction to Angular for .NET Developers
 
Introduction to Angular for .NET Developers
Introduction to Angular for .NET DevelopersIntroduction to Angular for .NET Developers
Introduction to Angular for .NET Developers
 
Introduction à Angular 2
Introduction à Angular 2Introduction à Angular 2
Introduction à Angular 2
 
ngconf 2016 (french)
ngconf 2016 (french)ngconf 2016 (french)
ngconf 2016 (french)
 
Microsoft Edge pour les développeurs web
Microsoft Edge pour les développeurs webMicrosoft Edge pour les développeurs web
Microsoft Edge pour les développeurs web
 
Microsoft Edge pour les développeurs web
Microsoft Edge pour les développeurs webMicrosoft Edge pour les développeurs web
Microsoft Edge pour les développeurs web
 
Xamarin.Forms [french]
Xamarin.Forms [french]Xamarin.Forms [french]
Xamarin.Forms [french]
 
Back from Xamarin Evolve 2014
Back from Xamarin Evolve 2014Back from Xamarin Evolve 2014
Back from Xamarin Evolve 2014
 
Windows App Studio
Windows App StudioWindows App Studio
Windows App Studio
 
Windows 8: Live tiles, badges et notifications toasts [french]
Windows 8: Live tiles, badges et notifications toasts [french]Windows 8: Live tiles, badges et notifications toasts [french]
Windows 8: Live tiles, badges et notifications toasts [french]
 
L'opportunité Windows 8 pour les développeurs
L'opportunité Windows 8 pour les développeursL'opportunité Windows 8 pour les développeurs
L'opportunité Windows 8 pour les développeurs
 
Building apps for WP8 and Win8
Building apps for WP8 and Win8Building apps for WP8 and Win8
Building apps for WP8 and Win8
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Último (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 

Introduction to SPAs with AngularJS

  • 1. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Introduction to SPAs with AngularJS @LaurentDuveau AngularAcademy.ca
  • 2. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 I have been creating web sites for 20 years… 1995 My JavaScript won’t work in Netscape! 2015 Still doing JavaScript… … but with
  • 3. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Agenda • SPA ? • AngularJS ? • Setup and first project • Modules • Controllers • Directives • Filters • Navigation with Routing • Services • REST API
  • 4. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 SPA ?
  • 5. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Single Page Application Not an app with a single page… Different views loaded into a screen without reloading everything from scratch + routing, history, two-way data binding, dependency injection, … SPA are client-centric application… so coded with JavaScript … lots of JavaScript… rely on a Framework!
  • 6. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 AngularJS ? 100% JavaScript Framework Created in October 2010 by developers inside Google Final first version in June 2012 Open Source, MIT license Compatible with IE 9+ and others modern browsers
  • 7. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Wait… another Js framework ?
  • 8. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Another Js framework, really !?? “there are more JavaScript frameworks than JavaScript developers”
  • 9. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Js frameworks… Google Trends http://www.google.com/trends/explore?hl=en#q=backbone.js,+angularjs,+ember.js,+knockout.js &cmpt=q
  • 10. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 SETUP AND FIRST SAMPLE!
  • 11. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Setup index.html
  • 12. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 First sample index.htmlbinding directives
  • 13. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 The ng-* attributes Angular Directives • Custom HTML elements (standard!) • Ignored by browsers, processed by Angular • Manage events and DOM interactions • Can have its own controller and template
  • 14. © LDEX, 2015 JavaScript Open Day Montreal, November 2015
  • 15. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 MODULES
  • 16. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Modules • Angular code is written in modules • Better decoupling, maintenance and testability • A module can use other modules dependenciesmodule name AngularJS!
  • 17. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Modules index.html app.js
  • 18. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Modules index.html app.js runs this module when page load dependencies none, for now…
  • 19. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 CONTROLLERS
  • 20. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Controller • Behavior of the application specified in controllers with functions and variables controller in the module good practice: write your code in an Immediately Invoked Function Expression (IIFE) app.js
  • 21. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Controller index.html directive controller name alias controller scope binding app.js
  • 22. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Controller index.html directive controller name alias controller scope binding app.js
  • 23. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 DEMO!
  • 24. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 DIRECTIVES
  • 25. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Directives • ng-model directive
  • 26. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Directives • Add a button with ng-click index.html
  • 27. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Directives • Add a button with ng-click index.html
  • 28. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Directives • ng-disabled directive app.js index.html
  • 29. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Directives • ng-disabled directive app.js index.html
  • 30. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Directives • ng-hide directive index.html app.js
  • 31. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Directives products array app.js
  • 32. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Directives • ng-repeat directive index.html
  • 33. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Directives • ng-repeat directive index.html
  • 34. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Directives • ng-class directive index.html styles.css
  • 35. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Directives • ng-class directive index.html styles.css
  • 36. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 FILTERS
  • 37. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Filters index.html formats for currency, date, … app.js
  • 38. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Filters index.html formats for currency, date, … app.js
  • 39. © LDEX, 2015 JavaScript Open Day Montreal, November 2015
  • 40. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Bidirectional data binding! When ng-click changes… …the expression {{store.displayLimit}} (and limitTo) is automatically updated!
  • 41. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 ROUTING
  • 42. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Routing ? • Split a page in views and navigate between them. About Us View ng-view (placeholder) Index.html My App Contact Us View #/About #/Contact
  • 43. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Routing with ngRoute • Import angular-route.js • Inject ngRoute into the module • Define the routes var app = angular.module('store', ['store-products', 'ngRoute']); app.config(["$routeProvider", function ($routeProvider) { $routeProvider.when("/", { controller: "StoreController", controllerAs: "storeCtrl", templateUrl: "/views/productsList.html" }) .when("/product/:id", { controller: "ProductController", controllerAs: "productCtrl", templateUrl: "/views/productView.html" }) .otherwise({ redirectTo: "/" }); }]); mysite.com/index.html#/product/1 mysite.com/index.html everything else…
  • 44. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Routing with ngRoute • Get Route parameter in the controller • Use ngView in your page <body> <div ng-view=""></div> . . . </body> index.html app.controller('ProductController', function ($routeParams, dataService) { dataService.getProductById($routeParams.id) .then(function (product) { . . . products.js
  • 45. © LDEX, 2015 JavaScript Open Day Montreal, November 2015
  • 46. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 SERVICES
  • 47. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Services • Shares functionality and data between controllers • Avoids code redundancy • Implemented as singletons (single instance) • AngularJS comes with built-in services ($http, $route, $log, $q, $resource, etc.)
  • 48. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 $resource and REST API angular.module('store', ['ngResource']) .factory('Product', function($resource) { return $resource('/api/product/:id'); }) .controller('MainCtrl', function(Product) { var vm = this; // Get all products vm.products = Product.query(); // Form data for creating a new product with ng-model vm.productData = {}; vm.newProduct = function() { var product = new Product(vm.productData); product.$save(); } }); also get(), remove(), delete()
  • 49. © LDEX, 2015 JavaScript Open Day Montreal, November 2015
  • 50. © LDEX, 2015 JavaScript Open Day Montreal, November 2015
  • 51. © LDEX, 2015 JavaScript Open Day Montreal, November 2015 Angular Training: 3-day course in Canada!