SlideShare une entreprise Scribd logo
1  sur  99
Télécharger pour lire hors ligne
The Ultimate
Introduction to
AngularJS
Jacopo Nardiello
Padawan Programmer
Twitter: @jnardiello
Practicing the force at
What Angular is NOT
“AngularJS is not jQuery”
What Angular is NOT
Framework != Library
“A framework embodies some abstract
design, with more behavior built in.”
! - M. Fowler
MVC MVVMvs
What is Angular
Superheroic JavaScript MVW Framework
What is Angular
Superheroic JavaScript MVW Framework
Model
What is Angular
Superheroic JavaScript MVW Framework
View
Model
What is Angular
Superheroic JavaScript MVW Framework
View
Model
Whatever (works for you)
Zen of Angular
#1
“It is a very good idea to decouple
DOM manipulation from app logic.”
#2
“It is a really, really good idea to regard
app testing as equal in importance to
app writing”
#3
“It is an excellent idea to decouple the
client side of an app from the server
side”
Elements of an Angular App
Bootstrapping an App
<body ng-app=“car">
JS bootstrap code
angular.module('car', [
'car.service',
'car.directive',
‘car.controller’
])
JS bootstrap code
angular.module('car', [
'car.service',
'car.directive',
‘car.controller’
])
JS bootstrap code
angular.module('car', [
'car.service',
'car.directive',
‘car.controller’
])
JS bootstrap code
angular.module('car', [
'car.service',
'car.directive',
‘car.controller’
])
Angular is modular
angular.module('car.service', […])
angular.module(‘car.directives', […])
Extending the DOM
Angular extends DOM
behavior using Directives
Directives
angular.module('greetApp', [])
.directive(‘greetMe’, function() {
return {
template: ‘<h1>Hello</h1>’
}
})
angular.module('greetApp', [])
.directive(‘greetMe’, function() {
return {
template: ‘<h1>Hello</h1>’
}
})
Directives
js
html <div greet-me></div>
angular.module('greetApp', [])
.directive(‘greetMe’, function() {
return {
template: ‘<h1>Hello</h1>’
}
})
Directives
Adding logic to directives
.directive('myCoffeeMachine', [
‘$interval’,
‘water’,
‘pump’,
function($interval, dateFilter) {
return {
restrict: 'AE',
link: function(scope, element, attrs) {
coffee = $interval(function() {
prepareCoffee(); // update DOM
}, n0000);
}
}]);
Adding logic to directives
.directive('myCoffeeMachine', [
‘$interval’,
‘water’,
‘pump’,
function($interval, dateFilter) {
return {
restrict: ‘AE',
link: function(scope, element, attrs) {
coffee = $interval(function() {
prepareCoffee(); // update DOM
}, n0000);
}
}]);
Adding logic to directives
.directive('myCoffeeMachine', [
‘$interval’,
‘water’,
‘pump’,
function($interval, dateFilter) {
return {
restrict: 'AE',
link: function(scope, element, attrs) {
coffee = $interval(function() {
prepareCoffee();
}, n0000);
}
}]);
Reusable element
.directive('myCoffeeMachine', [
‘$interval’,
‘water’,
‘pump’,
function($interval, dateFilter) {
return {
restrict: 'AE',
link: function(scope, element, attrs) {
coffee = $interval(function() {
prepareCoffee(); // update DOM
}, n0000);
}
}]);
<my-coffee-machine></..>
Controllers
“A Controller is a JavaScript constructor
function that is used to augment the Angular
Scope”
Controllers
“A Controller is a JavaScript constructor
function that is used to augment the Angular
Scope”
A step back: Scopes
Scope ~ Model ~ Current state
A step back: Scopes
Scope ~ Model ~ Current state
Scopes are objects organized hierarchically,
aimed to mimic the DOM structure
A step back: Scopes
Each module instantiate a rootScope object
Scope ~ Model ~ Current state
Scopes are objects organized hierarchically,
aimed to mimic the DOM structure
A step back: Scopes
Controllers
var myApp = angular.module('myApp',[]);
!
myApp.controller('GreetingController', [
'$scope',
function($scope) {
$scope.greeting = 'Hola!';
}]);
js
Controllers
var myApp = angular.module('myApp',[]);
!
myApp.controller('GreetingController', [
'$scope',
function($scope) {
$scope.greeting = 'Hola!';
}]);
<div ng-controller="GreetingController">
js
html
Angular automagically keeps in sync
the model inside controllers with views
Ultimate Data Binding
myApp.controller('GreetingController', [
'$scope',
function($scope) {
$scope.greeting = 'Hola!';
}]);
<div ng-controller="GreetingController">
{{ greeting }}
</div>
Ultimate Data Binding
js
html
Behavior everywhere
(and shared resources)
What if you want to declare a class, an object
or a function and have it available everywhere?
Behavior everywhere
(and shared resources)
Factories
Behavior everywhere
(and shared resources)
Factories
Services
Factory Example
.factory(‘addOne', function($window) {
var sum = 0
return function(sum) {
sum++
$window.alert(sum)
}
})
.factory(‘addOne', function($window) {
var sum = 0
return function(sum) {
sum++
$window.alert(sum)
}
})
Factory Example
Behavior everywhere
(and shared resources)
Factories
Services
returned value
of function
instance
Mounting all the pieces
Testing Angular
Misko Hevery
“Agile Coach at Google where he is
responsible for coaching Googlers to
maintain the high level of automated
testing culture”
- misko.hevery.com/about/
Misko Hevery
+
“Angular is written with
testability in mind”
- Angular Doc
Why is Angular easily
testable?
Dependency
Injection
DI
As a Pattern Framework
DI as Pattern
function Car() {
var wheel = new Wheel();
var engine = Engine.getInstance();
var door = app.get(‘Door’);
!
this.move = function() {
engine.on();
wheel.rotate();
door.open();
}
}
DI as Pattern
function Car() {
var wheel = new Wheel();
var engine = Engine.getInstance();
var door = app.get(‘Door’);
!
this.move = function() {
engine.on();
wheel.rotate();
door.open();
}
}
DI as Pattern
function Car(wheel, engine, door) {
this.move = function() {
engine.on();
wheel.rotate();
door.open();
}
}
The problem
function main() {
var fuel = new Fuel();
var electricity = new Electricity();
var engine = new Engine(fuel);
var door = new Door(Electricity);
var wheel = new Wheel();
var car = new Car(wheel, engine, door);
car.move();
}
The problem
function main() {
var fuel = new Fuel();
var electricity = new Electricity();
var engine = new Engine(fuel);
var door = new Door(Electricity);
var wheel = new Wheel();
var car = new Car(wheel, engine, door);
car.move();
}
The problem
function main() {
var fuel = new Fuel();
var electricity = new Electricity();
var engine = new Engine(fuel);
var door = new Door(Electricity);
var wheel = new Wheel();
var car = new Car(wheel, engine, door);
car.move();
}
The problem
function main() {
var fuel = new Fuel();
var electricity = new Electricity();
var engine = new Engine(fuel);
var door = new Door(Electricity);
var wheel = new Wheel();
var car = new Car(wheel, engine, door);
car.move();
}
DI as framework
function main() {
var injector = new Injector(….);
var car = injector.get(Car);
car.move();
}
DI as framework
function main() {
var injector = new Injector(….);
var car = injector.get(Car);
car.move();
}
Car.$inject = [‘wheel’, ‘engine’, ‘door’];
function Car(wheel, engine, door) {
this.move = function() {
…
}
}
DI as framework
function main() {
var injector = new Injector(….);
var car = injector.get(Car);
car.move();
}
Car.$inject = [‘wheel’, ‘engine’, ‘door’];
function Car(wheel, engine, door) {
this.move = function() {
…
}
}
Angular testability
is super-heroic!
but…
“you still have to do the right thing.”
- Angular Doc
Testability
1. Don’t use new
2. Don’t use globals
The Angular
Way
Solid structured
code
Testing components
Controller
function RocketCtrl($scope) {
$scope.maxFuel = 100;
$scope.finalCheck = function() {
if ($scope.currentFuel < $scope.maxFuel) {
$scope.check = ‘ko’;
} else {
$scope.check = 'ok';
}
};
}
Controller
function RocketCtrl($scope) {
$scope.maxFuel = 100;
$scope.finalCheck = function() {
if ($scope.currentFuel < $scope.maxFuel) {
$scope.check = ‘ko’;
} else {
$scope.check = 'ok';
}
};
}
var $scope = {};
var rc = $controller(
'RocketCtrl',
{ $scope: $scope }
);
!
$scope.currentFuel = 80;
$scope.finalCheck();
expect($scope.check).toEqual('ko');
Controller Test
Directive
app.directive('rocketLaunchPad', function () {
return {
restrict: 'AE',
replace: true,
template:
‘<rocket-launch-pad>
Rocket here
<rocket-launch-pad>’
};
});
Directive Test
it(‘Check launchpad was installed', function() {
var element = $compile(“<rocket-launch-pad></
rocket-launch-pad>”)($rootScope);
!
expect(element.html()).toContain("Rocket here");
});
Tools
Karma
Protractor
Test Runner
Karma
Run tests against real browsers
Karma
Run tests against real browsers
Unit tests
Config file
> karma init
Config file
> karma init
- frameworks: [‘jasmine’]
Config file
> karma init
- frameworks: [‘jasmine’]
- autoWatch: true
Config file
> karma init
- frameworks: [‘jasmine’]
- files: [
‘../tests/controllerSpec.js’
],
- autoWatch: true
Config file
> karma init
- frameworks: [‘jasmine’]
- files: [
‘../tests/controllerSpec.js’
],
- autoWatch: true
- browsers: ['Chrome']
Using Karma
> karma start config.js
Using Karma
> karma start config.js
Protractor
E2E Angular Testing
Angular wrapper for WebDriver
Anatomy of a E2E test
describe(‘…’, function() {
it(‘…’, function() {
browser.get(‘…’);
element(by.model(‘…’)).sendKeys(..);
!
var calculate = element(by.binding(‘…’));
!
expect(some.method()).toEqual(..);
});
});
Global Variables
describe(‘…’, function() {
it(‘…’, function() {
browser.get(‘…’);
element(by.model(‘…’)).sendKeys(..);
!
var calculate = element(by.binding(‘…’));
!
expect(some.method()).toEqual(..);
});
});
Global Variables
describe(‘…’, function() {
it(‘…’, function() {
browser.get(‘…’);
element(by.model(‘…’)).sendKeys(..);
!
var calculate = element(by.binding(‘…’));
!
expect(some.method()).toEqual(..);
});
});
Super-Happy Panda!
Page Objects
Protractor provides
Page Objects
Protractor provides
Debugging with superpowers
Page Objects
Protractor provides
Debugging with superpowers
Angular-specific functions
Final Thoughts
Jacopo Nardiello
Twitter: @jnardiello
Questions?

Contenu connexe

Tendances

Redux with angular 2 - workshop 2016
Redux with angular 2 - workshop 2016Redux with angular 2 - workshop 2016
Redux with angular 2 - workshop 2016Nir Kaufman
 
Promises are so passé - Tim Perry - Codemotion Milan 2016
Promises are so passé - Tim Perry - Codemotion Milan 2016Promises are so passé - Tim Perry - Codemotion Milan 2016
Promises are so passé - Tim Perry - Codemotion Milan 2016Codemotion
 
Opinionated AngularJS
Opinionated AngularJSOpinionated AngularJS
Opinionated AngularJSprabhutech
 
AngularJS Animations
AngularJS AnimationsAngularJS Animations
AngularJS AnimationsEyal Vardi
 
How to build an AngularJS backend-ready app WITHOUT BACKEND
How to build an AngularJS backend-ready app WITHOUT BACKEND How to build an AngularJS backend-ready app WITHOUT BACKEND
How to build an AngularJS backend-ready app WITHOUT BACKEND Enrique Oriol Bermúdez
 
AngularJS Internal
AngularJS InternalAngularJS Internal
AngularJS InternalEyal Vardi
 
AngularJS $Provide Service
AngularJS $Provide ServiceAngularJS $Provide Service
AngularJS $Provide ServiceEyal Vardi
 
Building scalable applications with angular js
Building scalable applications with angular jsBuilding scalable applications with angular js
Building scalable applications with angular jsAndrew Alpert
 
Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React Robert DeLuca
 
Upgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.xUpgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.xEyal Vardi
 
React Native - Workshop
React Native - WorkshopReact Native - Workshop
React Native - WorkshopFellipe Chagas
 
AngularJs $provide API internals & circular dependency problem.
AngularJs $provide API internals & circular dependency problem.AngularJs $provide API internals & circular dependency problem.
AngularJs $provide API internals & circular dependency problem.Yan Yankowski
 
Why angular js Framework
Why angular js Framework Why angular js Framework
Why angular js Framework Sakthi Bro
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJSWei Ru
 
Workshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte IWorkshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte IVisual Engineering
 
AngularJS Architecture
AngularJS ArchitectureAngularJS Architecture
AngularJS ArchitectureEyal Vardi
 

Tendances (20)

Redux with angular 2 - workshop 2016
Redux with angular 2 - workshop 2016Redux with angular 2 - workshop 2016
Redux with angular 2 - workshop 2016
 
Promises are so passé - Tim Perry - Codemotion Milan 2016
Promises are so passé - Tim Perry - Codemotion Milan 2016Promises are so passé - Tim Perry - Codemotion Milan 2016
Promises are so passé - Tim Perry - Codemotion Milan 2016
 
Solid angular
Solid angularSolid angular
Solid angular
 
Opinionated AngularJS
Opinionated AngularJSOpinionated AngularJS
Opinionated AngularJS
 
AngularJS Animations
AngularJS AnimationsAngularJS Animations
AngularJS Animations
 
How to build an AngularJS backend-ready app WITHOUT BACKEND
How to build an AngularJS backend-ready app WITHOUT BACKEND How to build an AngularJS backend-ready app WITHOUT BACKEND
How to build an AngularJS backend-ready app WITHOUT BACKEND
 
AngularJS Internal
AngularJS InternalAngularJS Internal
AngularJS Internal
 
AngularJS $Provide Service
AngularJS $Provide ServiceAngularJS $Provide Service
AngularJS $Provide Service
 
AngularJS Basics with Example
AngularJS Basics with ExampleAngularJS Basics with Example
AngularJS Basics with Example
 
Building scalable applications with angular js
Building scalable applications with angular jsBuilding scalable applications with angular js
Building scalable applications with angular js
 
Angular js meetup
Angular js meetupAngular js meetup
Angular js meetup
 
Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React
 
Upgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.xUpgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.x
 
React Native - Workshop
React Native - WorkshopReact Native - Workshop
React Native - Workshop
 
AngularJs $provide API internals & circular dependency problem.
AngularJs $provide API internals & circular dependency problem.AngularJs $provide API internals & circular dependency problem.
AngularJs $provide API internals & circular dependency problem.
 
Why angular js Framework
Why angular js Framework Why angular js Framework
Why angular js Framework
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
 
Workshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte IWorkshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte I
 
Angularjs Performance
Angularjs PerformanceAngularjs Performance
Angularjs Performance
 
AngularJS Architecture
AngularJS ArchitectureAngularJS Architecture
AngularJS Architecture
 

Similaire à Ultimate Introduction To AngularJS

Good karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with KarmaGood karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with KarmaExoLeaders.com
 
Coffee@DBG - Exploring Angular JS
Coffee@DBG - Exploring Angular JSCoffee@DBG - Exploring Angular JS
Coffee@DBG - Exploring Angular JSDeepu S Nath
 
Angular JS Unit Testing - Overview
Angular JS Unit Testing - OverviewAngular JS Unit Testing - Overview
Angular JS Unit Testing - OverviewThirumal Sakthivel
 
Patterns Are Good For Managers
Patterns Are Good For ManagersPatterns Are Good For Managers
Patterns Are Good For ManagersAgileThought
 
Building an End-to-End AngularJS Application
Building an End-to-End AngularJS ApplicationBuilding an End-to-End AngularJS Application
Building an End-to-End AngularJS ApplicationDan Wahlin
 
Introduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress DevelopersIntroduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress DevelopersCaldera Labs
 
Angular 4 for Java Developers
Angular 4 for Java DevelopersAngular 4 for Java Developers
Angular 4 for Java DevelopersYakov Fain
 
How AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design PatternsHow AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design PatternsRan Mizrahi
 
How to call $scope function from console
How to call $scope function from consoleHow to call $scope function from console
How to call $scope function from consolecodeandyou forums
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasminePaulo Ragonha
 
243329387 angular-docs
243329387 angular-docs243329387 angular-docs
243329387 angular-docsAbhi166803
 
AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014Dariusz Kalbarczyk
 
AngularJS Basics
AngularJS BasicsAngularJS Basics
AngularJS BasicsRavi Mone
 

Similaire à Ultimate Introduction To AngularJS (20)

Testing AngularJS
Testing AngularJSTesting AngularJS
Testing AngularJS
 
Good karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with KarmaGood karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with Karma
 
Coffee@DBG - Exploring Angular JS
Coffee@DBG - Exploring Angular JSCoffee@DBG - Exploring Angular JS
Coffee@DBG - Exploring Angular JS
 
Angular js
Angular jsAngular js
Angular js
 
Angular JS Unit Testing - Overview
Angular JS Unit Testing - OverviewAngular JS Unit Testing - Overview
Angular JS Unit Testing - Overview
 
Intro to AngularJs
Intro to AngularJsIntro to AngularJs
Intro to AngularJs
 
Patterns Are Good For Managers
Patterns Are Good For ManagersPatterns Are Good For Managers
Patterns Are Good For Managers
 
Building an End-to-End AngularJS Application
Building an End-to-End AngularJS ApplicationBuilding an End-to-End AngularJS Application
Building an End-to-End AngularJS Application
 
Introduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress DevelopersIntroduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress Developers
 
Angular
AngularAngular
Angular
 
Angular
AngularAngular
Angular
 
Angular js - the beginning
Angular js - the beginningAngular js - the beginning
Angular js - the beginning
 
Angular 4 for Java Developers
Angular 4 for Java DevelopersAngular 4 for Java Developers
Angular 4 for Java Developers
 
How AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design PatternsHow AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design Patterns
 
Intro to ReactJS
Intro to ReactJSIntro to ReactJS
Intro to ReactJS
 
How to call $scope function from console
How to call $scope function from consoleHow to call $scope function from console
How to call $scope function from console
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
 
243329387 angular-docs
243329387 angular-docs243329387 angular-docs
243329387 angular-docs
 
AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014
 
AngularJS Basics
AngularJS BasicsAngularJS Basics
AngularJS Basics
 

Plus de Jacopo Nardiello

The Art of Cloud Native Defense on Kubernetes
The Art of Cloud Native Defense on KubernetesThe Art of Cloud Native Defense on Kubernetes
The Art of Cloud Native Defense on KubernetesJacopo Nardiello
 
Tales of the mythical cloud-native platform - Container day 2022
Tales of the mythical cloud-native platform - Container day 2022Tales of the mythical cloud-native platform - Container day 2022
Tales of the mythical cloud-native platform - Container day 2022Jacopo Nardiello
 
Monitoring Cloud Native Applications with Prometheus
Monitoring Cloud Native Applications with PrometheusMonitoring Cloud Native Applications with Prometheus
Monitoring Cloud Native Applications with PrometheusJacopo Nardiello
 
Eventsourcing with PHP and MongoDB
Eventsourcing with PHP and MongoDBEventsourcing with PHP and MongoDB
Eventsourcing with PHP and MongoDBJacopo Nardiello
 

Plus de Jacopo Nardiello (7)

The Art of Cloud Native Defense on Kubernetes
The Art of Cloud Native Defense on KubernetesThe Art of Cloud Native Defense on Kubernetes
The Art of Cloud Native Defense on Kubernetes
 
Tales of the mythical cloud-native platform - Container day 2022
Tales of the mythical cloud-native platform - Container day 2022Tales of the mythical cloud-native platform - Container day 2022
Tales of the mythical cloud-native platform - Container day 2022
 
Breaking the monolith
Breaking the monolithBreaking the monolith
Breaking the monolith
 
Monitoring Cloud Native Applications with Prometheus
Monitoring Cloud Native Applications with PrometheusMonitoring Cloud Native Applications with Prometheus
Monitoring Cloud Native Applications with Prometheus
 
Kubernetes 101
Kubernetes 101Kubernetes 101
Kubernetes 101
 
Becoming a developer
Becoming a developerBecoming a developer
Becoming a developer
 
Eventsourcing with PHP and MongoDB
Eventsourcing with PHP and MongoDBEventsourcing with PHP and MongoDB
Eventsourcing with PHP and MongoDB
 

Ultimate Introduction To AngularJS