51
© LDEX, 2015 JavaScript Open Day Montreal, November 2015 Introduction to SPAs with AngularJS @LaurentDuveau AngularAcademy.ca

Introduction to SPAs with AngularJS

Embed Size (px)

Citation preview

Page 1: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Introduction to SPAs with

AngularJS

@LaurentDuveauAngularAcademy.ca

Page 2: Introduction to SPAs with AngularJS

© 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

Page 3: Introduction to SPAs with AngularJS

© 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

Page 4: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

SPA ?

Page 5: Introduction to SPAs with AngularJS

© 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, dependencyinjection, …

SPA are client-centric application… so coded withJavaScript

… lots of JavaScript… rely on a Framework!

Page 6: Introduction to SPAs with AngularJS

© 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

Page 7: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Wait… another Js framework ?

Page 8: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Another Js framework, really !??

“there are more JavaScript frameworks

than JavaScript developers”

Page 9: Introduction to SPAs with AngularJS

© 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

Page 10: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

SETUP AND FIRST SAMPLE!

Page 11: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Setup

index.html

Page 12: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

First sample

index.htmlbinding

directives

Page 13: Introduction to SPAs with AngularJS

© 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

Page 14: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Page 15: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

MODULES

Page 16: Introduction to SPAs with AngularJS

© 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

dependenciesmodulename

AngularJS!

Page 17: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Modules

index.html

app.js

Page 18: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Modules

index.html

app.js

runs this module when page load

dependenciesnone, for now…

Page 19: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

CONTROLLERS

Page 20: Introduction to SPAs with AngularJS

© 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

Page 21: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Controller

index.html

directive controller name alias

controllerscope

binding

app.js

Page 22: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Controller

index.html

directive controller name alias

controllerscope

binding

app.js

Page 23: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

DEMO!

Page 24: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

DIRECTIVES

Page 25: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Directives

• ng-model directive

Page 26: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Directives

• Add a button with ng-click

index.html

Page 27: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Directives

• Add a button with ng-click

index.html

Page 28: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Directives

• ng-disabled directive

app.js

index.html

Page 29: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Directives

• ng-disabled directive

app.js

index.html

Page 30: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Directives

• ng-hide directive

index.html

app.js

Page 31: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Directives

products array

app.js

Page 32: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Directives

• ng-repeat directive

index.html

Page 33: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Directives

• ng-repeat directive

index.html

Page 34: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Directives

• ng-class directive

index.html

styles.css

Page 35: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Directives

• ng-class directive

index.html

styles.css

Page 36: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

FILTERS

Page 37: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Filters

index.html

formats for currency, date, …

app.js

Page 38: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Filters

index.html

formats for currency, date, …

app.js

Page 39: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Page 40: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Bidirectional data binding!

When ng-click changes…

…the expression {{store.displayLimit}} (and limitTo) is

automatically updated!

Page 41: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

ROUTING

Page 42: Introduction to SPAs with AngularJS

© 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

Page 43: Introduction to SPAs with AngularJS

© 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…

Page 44: Introduction to SPAs with AngularJS

© 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

Page 45: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Page 46: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

SERVICES

Page 47: Introduction to SPAs with AngularJS

© 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.)

Page 48: Introduction to SPAs with AngularJS

© 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 productsvm.products = Product.query();

// Form data for creating a new product with ng-modelvm.productData = {};vm.newProduct = function() {var product = new Product(vm.productData);product.$save();

}});

also get(), remove(), delete()

Page 49: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Page 50: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Page 51: Introduction to SPAs with AngularJS

© LDEX, 2015 JavaScript Open Day Montreal, November 2015

Angular Training: 3-day course in Canada!