SlideShare a Scribd company logo
Front End Workshops
XII. AngularJS - Part 3
Juan Luís Marí
jlmari@visual-engin.com
Pablo Balduz
pbalduz@visual-engin.com
Héctor Canto
hcanto@visual-engin.com
Overview
“AngularJS is turn-based”
In previous chapters...
● AngularJS intro
● Modules and dependency injection
● Filters, Controllers and Services
● Routes
● Digest cycle
● Data binding
● Directives ...
Overview
● Scope functions
● Patterns
● Testing
● Directives functions (continuation of custom directives)
○ compile, controller and link functions
● Build-in directives
Scope functions
— Watch, apply & Digest—
Scope functions
Watch, apply and digest are $scope functions used to monitor and process
binded data in terms of the digest cycle.
There are used very scarcely but it is good to know in order to understand bad
updates, performance issues and digest loop errors.
http://tutorials.jenkov.com/angularjs/watch-digest-apply.html
Scope functions - watch
Data binding creates ‘watchers’ internally. Every digest cycle will check
watched variables for changes and react accordingly.
Variables are watched implicitly when we use double curly brackets:
{{myVar}}
and explicitly:
$scope.watch(‘myVar’, function(){...});
As 1st parameter we can call a function, also.
The 2nd function is called a ‘listener’.
http://tutorials.jenkov.com/angularjs/watch-digest-apply.html
Scope functions - apply
Through the digest cycle, watched variables will be monitored.
But sometimes we don’t have a variable to watch and we are waiting for an
async event.
Some directives and services already use apply for us internally.
● ng-click
● $timeout
● $http
and explicitly:
http://jimhoskins.com/2012/12/17/angularjs-and-apply.html
Scope functions - apply
function Ctrl($scope) {
$scope.message = "Waiting 2000ms for update";
setTimeout(function () {
$scope.$apply(function () {
$scope.message = "Timeout called!";
});
}, 2000);
}
Note: Instead, you could use $timeout.
Scope functions - apply
When to use apply:
● Very rarely ( or better NEVER ). Two well-know cases:
○ dealing with sockets
○ wrapping non-Angular code
○ when you know a variable is initialized outside the digest cycle
How to use apply:
● with a function preferably:
$scope.apply( function() {});
● Tip: Use $timeout without delay (but it will be defered).
http://stackoverflow.com/questions/23070822/angular-scope-apply-vs-timeout-as-a-safe-apply
Scope functions - digest
$scope.digest triggers a digest cycle, but we should never call it directly.
● digest is triggered internally
● after the execution of $scope.apply, at the end of its execution
We use apply (and therefore digest) when we don’t have a variable to watch
and we are waiting for an async event.
http://jimhoskins.com/2012/12/17/angularjs-and-apply.html
Common Patterns
— non trivial only—
Known patterns
Angular is already build as a collection of patterns
● Services are singletons.
● Factories are factories.
● Data-binding and $scope.watch implements a watcher pattern.
● Controller and Template.
● Dependency injections: everywhere.
http://blog.mgechev.com/2014/05/08/angularjs-in-patterns-part-1-overview-of-angularjs/
Observer (publish-subscribe)
Observers are similar to watchers, but instead of permanently watch a fuction
they subscribe to events.
To subscribe:
$scope.on(‘event-name’, function handler() {
//your code here
}
And to launch the event:
function ExampleCtrl($scope) {
$scope.$emit('event-name', { foo: 'bar' }); //upwards on the scope hierarchy
$scope.$broadcast('event-name', { foo: 'bar' }); //downwards to all child
$rootScope.$broadcast('event-name', { foo: 'bar' }); //to everybody
}
http://blog.mgechev.com/2014/07/05/angularjs-in-patterns-part-3/
Event listener (View observer)
Multiple build-in directives are event listeners:
● ng-click, ng-dbl-click
● ng-mouse…
● ng-keydown, ng-keyup
● ng-change
You can add your own event listeners. Let see an example:
http://tutorials.jenkov.com/angularjs/events.html
Event listener example
angular.module('myApp', [])
.directive('myDirective', function myDirective () {
return {
restrict: 'AE',
link: myDirectiveLink
} });
function myDirectiveLink (scope, element, attrs) {
var domElement = element[0]; // Get the native JS element
domElement.addEventListener("dragstart", myEventListener, false); // You can use ngDraggable but it uses jQuery
}
function myEventListener () {
// Handle event here
}
Other patterns
Proxy
● a service using $http or $resource (Restful http) internally.
Data Mapper
● a service returning Model instances calling $http or $resource.
Testing
— AngularJS embraces testing—
AngularJS takes well care of testing:
● Well supported by Karma (test runner)
● Can use Mocha, Sinon, Chai ...
● Protractor and ngMock specially build for testing Angular.
● KEY: inject dependencies and mock key elements
Testing in AngularJS
https://www.smashingmagazine.com/2014/10/introduction-to-unit-testing-in-angularjs/
https://quickleft.com/blog/angularjs-unit-testing-for-real-though/
E2E framework based on Jasmine and Selenium’s WebDriver
Ability to simulate user interaction
Simulation in selenium and most browsers
Usually, we will run tests in Selenium (without UI) and all target browsers.
We can automate this with Gulp
Protractor
Provides several services to mock Angular behaviour:
● $timeout,
● $controller : inject controllers
● $httpBackend: patch or mock $http calls
● module() and inject():
○ resolve dependencies on tests
○ patch methods
○ mock scope
ngMock
http://www.bradoncode.com/blog/2015/05/27/ngmock-fundamentals-angularjs-testing-inject/
Unit Testing
● Testing individual, small units of code → Need of isolated code
○ Note: Bad isolated code may force the app to create related pieces as server’s
requests or new DOM elements → Affect other tests and produce errors
● Solutions: Dependency Injection and Mocking Services
○ DOM elements abstracted → never directly modified
○ XHR requests and petitions → simulated (by dependency injection of $httpBackend)
■ All we really need is to verify whether a certain request has been sent or not, or
alternatively just let the application make requests, respond with pre-trained
responses and assert that the end result is what we expect it to be.
Dependency Injection
● AngularJS built-in. Allows to pass in a
component's dependencies and stub or mock
them as you wish.
● Does not modify any global variable, so it does
not affect other tests
angular.module('app', [])
.controller(ExampleCtrl, function ExampleCtrl($scope) {
...
});
describe('ExampleCtrl tests', function() {
beforeEach(module('app'));
var $controller;
beforeEach(inject(function(_$controller_){
// The injector unwraps the underscores (_) from around the
parameter names when matching
$controller = _$controller_;
}));
describe('block to be tested', function() {
it('test1', function() {
// create a scope object for us to use.
var $scope = {};
// now run that scope through the controller function, injecting any
services or other injectables we need.
// **NOTE**: this is the only time the controller function will be
run, so anything that occurs inside of that will already be done before
the first spec.
var controller = $controller('ExampleController', { $scope: $scope
}); }; }); });
$httpBackend
● angular-mocks module allows to inject and mock the AngularJS services, so they can be extended
and used synchronously → $httpBackend is one of them
● Service in module ngMock
● Fake HTTP backend implementation suitable for unit testing applications that use the $http service.
● Allows not using external dependencies, so the requests to URLs are mocked.
● With dependency injection, it is easy to inject $httpBackend mock and use it to verify the requests
and respond with some testing data without sending a request to a real server.
● $httpBackend.flush() allows to flush pending requests, so the test will run synchronous but
preserving the async of the backend API
Custom directives - Continuation
— DOM Manipulation—
There are 3 types of functions, by order of execution:
○ compile, controller and link
● Compile happens once, before the template is compiled.
● The rest of functions is run once for each time the directive is used
■ For example in a ng-repeat of 4 elements, 4 loops
○ Controller initialize the scope.
○ Link happens when the linking is being made, by default after.
○ We can divide link into two, pre-link and post-link
■ Pre-link happens before the element is linked to the scope
■ Post-link happens just after, when the element affected is on the DOM.
● This is the most usual and potentially safest
Custom directives: functions
http://www.undefinednull.com/2014/07/07/practical-guide-to-prelink-postlink-and-controller-methods-of-angular-directives/
compile: function CompilingFunction($templateElement, $templateAttributes) {
…}
link: function LinkingFunction($scope, $element, $attributes) { ... }
…}
link: {
pre: function PreLinkingFunction($scope, $element, $attributes) { ... },
post: function PostLinkingFunction($scope, $element, $attributes) { ... },
}
Custom directives: functions
http://plnkr.co/edit/qrDMJBlnwdNlfBqEEXL2?p=preview
https://github.com/angular/angular.js/wiki/Understanding-Directives
Custom directives: link, prelink, postlink
● There are 4 arguments available for these functions (in this order)
○ scope, elements, attributes and controllers
● You can access the DOM, you have the element.
● By default use link directly, which is equivalent to post-link alone.
● Remember, if possible provide values as soon as you can.
○ Don’t wait to post-link, do it in the controller or in compile
● [post-]link is the View part where you have everything in place and you do
the last adjustments and decisions regarding the DOM.
Custom directives: link, prelink, postlink
var app = angular.module('app', []);
app.directive('dad', function () {
return {
restrict: 'EA',
template: '<div class="dad">{{greeting}}{{name}}'+
'<son></son>'+
'</div>',
link: {
pre: function(scope,elem,attr){
scope.name = 'Paul';
scope.greeting = 'Hey, I am ';
}
}
};
})
app.directive('son', function () {
return {
restrict: 'EA',
template: '<div class="son">{{sonSays}}</div>',
link: function(scope,elem,attr){
scope.sonSays = 'Hey, I am David, and my dad is
'+ scope.name;
}
};
});
<div ng-app="app">
<dad></dad>
</div>
Hey, I am Paul
Hey, I am David, and my dad is Paul
Custom directives: post-link,
● It is safe to manipulate the DOM in post-link as the element is already in
the DOM.
● It is possible to access the scope
● All child directives are linked so it’s safe to access them
○ their scope and the elements they affect.
● It is safe to attach events handlers to elements.
Custom directives: pre-link,
● Use of pre-link is scarce,
○ A child needs data from its parent
● Safe to attach an event to the DOM element
○ Not safe to access DOM elements from child directives
● The scope is not linked yet.
Custom directives: compile
● In this phase AngularJS manipulates the DOM of the HTML template
● Each directive has a chance to do some processing for all and each DOM
nodes it appears in.
● The scope is not attached yet.
● The template is still bare, without binding nor substitutions.
Built-in directives
Built-in directives
● ng-model
● ng-src
● ng-class
● ng-style
● ng-click
● ng-if
● ng-show
● ng-include
● ng-repeat
ng-model
● Binds inputs to scope properties
● If property does not exist, it is created and added to the scope
● Watches model by reference Important!
● Allows animation and validation
<script>
angular.module("module", [])
.controller("controller", ['$scope', function($scope) {
$scope.value = "Luis";
}]);
</script>
<div ng-controller="controller">
Name: <input ng-model="value"/>
</div>
ng-src
● src leads to problems when evaluating expressions
● Prevents browser from loading resources before an expression is
evaluated
Buggy way
Correct way
<img src="http://www.gravatar.com/avatar/{{hash}}" alt="Description"/>
<img ng-src="http://www.gravatar.com/avatar/{{hash}}" alt="Description" />
ng-class
● Allows to dynamically set CSS classes on an HTML
● Ways of operating (syntax):
○ String
○ Key-value pair object
○ Array of strings (type 1) or objects (type 2)
○ Ternary operator
● Usage options: class and attribute
ng-class
String syntax
Array syntax
Key-value pair syntax
<input type="text" ng-model="style">
<div ng-class="style">String syntax</div>
<input type="text" ng-model="styleOne">
<input type="text" ng-model="styleTwo">
<div ng-class="[styleOne, styleTwo]">Array syntax</div>
<input type="checkbox" ng-model="great"> Barça
<input type="checkbox" ng-model="poor"> Madrid
<div ng-class="{ styleOne: great, styleTwo: poor }">
ng-class
Ternary operator
Specific numbers
ngClass as a class
ngClass as an attribute (every example shown except the last one)
ng-class="variableToEvaluate ? 'class-if-true' : 'class-if-false'">
<ul><li ng-class="{ 'text-success': $first }" ng-repeat="item in items">{{ item.name }}</li></ul>
<div class="item ng-class:type;">Stuff</div>
<div class="item ng-class:[styleOne, styleTwo];">Stuff</div>
<div class="item ng-class:{ 'text-error': wrong };">Stuff</div>
ng-style
● Allows setting style on an HTML conditionally
● Interpolates javascript object into style attribute, not css class
Following directive will be translated to style=”color:red”
Following directive will be translated to class=”color”
For interpolation, instead of doing this:
do this:
ng-style="{color: 'red'}"
ng-class="{color: colorClass}"
style="width: {{progress}}"
ng-style="{width: progress}"
ng-click
● Specify custom behavior when an element is clicked
<button ng-click="count = 0">
Reset
</button>
<button ng-click="reset()">
Reset
</button>
ng-show
● Shows / hides HTML element based on an expression
● Adds / removes ng-hide class
● Animations
Element visible when $scope.value is truthy
Element hidden when $scope.value is falsy
<div ng-show="value"></div>
<div ng-show="value" class="ng-hide"></div>
ng-if
● Removes / recreates part of the DOM
● scope is removed and recreated
● ng-model with ng-if issues
● Animations
Similar usage to ngShow / ngHide
<div ng-if="value"></div>
ngAnimate
● Include angular-animate.js and load module
● Directives support
○ ngRepeat
○ ngShow
○ ngHide
○ ngIf
○ ...
● CSS / JS based animations
ngAnimate
CSS based animations
● No need of JavaScript code at all
● CSS class referenced between HTML and CSS
● 2 css classes added depending on the animation event
<div ng-if="bool" class="fade">
Fade me in out
</div>
<button ng-click="bool=true">Fade In!</button>
<button ng-click="bool=false">Fade Out!</button>
.fade.ng-enter {
transition:0.5s linear all;
opacity:0;
}
.fade.ng-enter.ng-enter-active {
opacity:1;
}
ngAnimate
CSS based animations
● Staggering animations
● ng-Animate css class
● Custom keyframe animations
.zipper.ng-animate {
transition:0.5s linear all;
}
.zipper.ng-enter {
opacity:0;
}
.zipper.ng-enter.ng-enter-active {
opacity:1;
}
.zipper.ng-leave {
opacity:1;
}
.zipper.ng-leave.ng-leave-active {
opacity:0;
}
ngAnimate
JS based animations
Register JavaScript animation on the module
<div ng-repeat="item in items" class="slide">
{{ item }}
</div>
myModule.animation('.slide', [function() {
return {
enter: function(element, doneFn) {
// Do some cool animation and call doneFn
},
move: function(element, doneFn) {},
leave: function(element, doneFn) {}
}
}]);
ngAnimate
ngAnimate documentation: https://docs.angularjs.org/api/ngAnimate
ng-include
● Add HTML code from external file to the current one
○ Fetches, compiles and includes an external HTML fragment
● Create new scope for the included template
● The included HTML files can also contain AngularJS code
<div ng-include="'myFile.html'"></div>
ng-include
● Cross-domain:
○ By default, the ng-include directive does not allow you to include
files from other domains.
○ To include files from another domain, you can add a whitelist of
legal files and/or domains in the config function of your application:
var app = angular.module('myApp', [])
app.config(function($sceDelegateProvider) {
$sceDelegateProvider.resourceUrlWhitelist([
'http://www.refsnesdata.no/**'
]);
});
ng-repeat
● Instantiates a template once per item from a collection
<div ng-repeat="item in collection"> ... </div>
● Each template instance gets its own scope
● It is also possible to get ngRepeat to iterate over the properties of an object
○ Note: The built-in filters orderBy and filter do not work with objects
<div ng-repeat="(key, value) in myObj"> ... </div>
ng-repeat
ng-repeat
● Continuously watching if changes in the collection happens
○ Automatically changing DOM (when adding, removing or reordering items)
○ “Keep track” items with their DOM elements → Avoids re-render them if not needed
● Default tracking function (changeable with track-by)
○ Tracks by identity of item
○ Does not allows duplicated items
● ng-repeat in more than one parent element of the HTML
○ ng-repeat-start
○ ng-repeat-end
range of the repeater
Workshop 14: AngularJS Parte III

More Related Content

What's hot

Building scalable applications with angular js
Building scalable applications with angular jsBuilding scalable applications with angular js
Building scalable applications with angular js
Andrew Alpert
 
Workshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & ReduxWorkshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & Redux
Visual Engineering
 
Workshop 24: React Native Introduction
Workshop 24: React Native IntroductionWorkshop 24: React Native Introduction
Workshop 24: React Native Introduction
Visual Engineering
 
How Angular2 Can Improve Your AngularJS Apps Today!
How Angular2 Can Improve Your AngularJS Apps Today!How Angular2 Can Improve Your AngularJS Apps Today!
How Angular2 Can Improve Your AngularJS Apps Today!
Nir Kaufman
 
AngularJS for Java Developers
AngularJS for Java DevelopersAngularJS for Java Developers
AngularJS for Java Developers
Loc Nguyen
 
Workshop 2: JavaScript Design Patterns
Workshop 2: JavaScript Design PatternsWorkshop 2: JavaScript Design Patterns
Workshop 2: JavaScript Design Patterns
Visual Engineering
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing options
Nir Kaufman
 
Angular 2.0 Routing and Navigation
Angular 2.0 Routing and NavigationAngular 2.0 Routing and Navigation
Angular 2.0 Routing and Navigation
Eyal Vardi
 
AngularJs
AngularJsAngularJs
AngularJs
syam kumar kk
 
AngularJS Internal
AngularJS InternalAngularJS Internal
AngularJS Internal
Eyal Vardi
 
Workshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native Side
Visual Engineering
 
Workshop 25: React Native - Components
Workshop 25: React Native - ComponentsWorkshop 25: React Native - Components
Workshop 25: React Native - Components
Visual Engineering
 
Using React, Redux and Saga with Lottoland APIs
Using React, Redux and Saga with Lottoland APIsUsing React, Redux and Saga with Lottoland APIs
Using React, Redux and Saga with Lottoland APIs
Mihail Gaberov
 
«От экспериментов с инфраструктурой до внедрения в продакшен»​
«От экспериментов с инфраструктурой до внедрения в продакшен»​«От экспериментов с инфраструктурой до внедрения в продакшен»​
«От экспериментов с инфраструктурой до внедрения в продакшен»​
FDConf
 
Angular2 & ngrx/store: Game of States
Angular2 & ngrx/store: Game of StatesAngular2 & ngrx/store: Game of States
Angular2 & ngrx/store: Game of States
Oren Farhi
 
AngularJS - Services
AngularJS - ServicesAngularJS - Services
AngularJS - Services
Nir Kaufman
 
AngularJs Crash Course
AngularJs Crash CourseAngularJs Crash Course
AngularJs Crash Course
Keith Bloomfield
 
Angular js 2
Angular js 2Angular js 2
Angular js 2
Ran Wahle
 
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...
Codemotion
 

What's hot (20)

Building scalable applications with angular js
Building scalable applications with angular jsBuilding scalable applications with angular js
Building scalable applications with angular js
 
Workshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & ReduxWorkshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & Redux
 
Workshop 24: React Native Introduction
Workshop 24: React Native IntroductionWorkshop 24: React Native Introduction
Workshop 24: React Native Introduction
 
How Angular2 Can Improve Your AngularJS Apps Today!
How Angular2 Can Improve Your AngularJS Apps Today!How Angular2 Can Improve Your AngularJS Apps Today!
How Angular2 Can Improve Your AngularJS Apps Today!
 
AngularJS for Java Developers
AngularJS for Java DevelopersAngularJS for Java Developers
AngularJS for Java Developers
 
Workshop 2: JavaScript Design Patterns
Workshop 2: JavaScript Design PatternsWorkshop 2: JavaScript Design Patterns
Workshop 2: JavaScript Design Patterns
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing options
 
Angular 2.0 Routing and Navigation
Angular 2.0 Routing and NavigationAngular 2.0 Routing and Navigation
Angular 2.0 Routing and Navigation
 
AngularJs
AngularJsAngularJs
AngularJs
 
AngularJS Internal
AngularJS InternalAngularJS Internal
AngularJS Internal
 
Workshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native Side
 
Workshop 25: React Native - Components
Workshop 25: React Native - ComponentsWorkshop 25: React Native - Components
Workshop 25: React Native - Components
 
Using React, Redux and Saga with Lottoland APIs
Using React, Redux and Saga with Lottoland APIsUsing React, Redux and Saga with Lottoland APIs
Using React, Redux and Saga with Lottoland APIs
 
«От экспериментов с инфраструктурой до внедрения в продакшен»​
«От экспериментов с инфраструктурой до внедрения в продакшен»​«От экспериментов с инфраструктурой до внедрения в продакшен»​
«От экспериментов с инфраструктурой до внедрения в продакшен»​
 
Angular2 & ngrx/store: Game of States
Angular2 & ngrx/store: Game of StatesAngular2 & ngrx/store: Game of States
Angular2 & ngrx/store: Game of States
 
Angularjs
AngularjsAngularjs
Angularjs
 
AngularJS - Services
AngularJS - ServicesAngularJS - Services
AngularJS - Services
 
AngularJs Crash Course
AngularJs Crash CourseAngularJs Crash Course
AngularJs Crash Course
 
Angular js 2
Angular js 2Angular js 2
Angular js 2
 
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...
 

Similar to Workshop 14: AngularJS Parte III

Angular js-crash-course
Angular js-crash-courseAngular js-crash-course
Angular js-crash-course
Keith Bloomfield
 
AngularJS in practice
AngularJS in practiceAngularJS in practice
AngularJS in practice
Eugene Fidelin
 
Angular workshop - Full Development Guide
Angular workshop - Full Development GuideAngular workshop - Full Development Guide
Angular workshop - Full Development Guide
Nitin Giri
 
Angular meetup 2 2019-08-29
Angular meetup 2   2019-08-29Angular meetup 2   2019-08-29
Angular meetup 2 2019-08-29
Nitin Bhojwani
 
Angular Intermediate
Angular IntermediateAngular Intermediate
Angular Intermediate
LinkMe Srl
 
AngularJs presentation
AngularJs presentation AngularJs presentation
AngularJs presentation
Phan Tuan
 
AngularJS Workshop
AngularJS WorkshopAngularJS Workshop
AngularJS Workshop
Gianluca Cacace
 
Intro to AngularJs
Intro to AngularJsIntro to AngularJs
Intro to AngularJs
SolTech, Inc.
 
Angularjs 131211063348-phpapp01
Angularjs 131211063348-phpapp01Angularjs 131211063348-phpapp01
Angularjs 131211063348-phpapp01
Arunangsu Sahu
 
Introduction to AngularJs
Introduction to AngularJsIntroduction to AngularJs
Introduction to AngularJs
murtazahaveliwala
 
Angular js
Angular jsAngular js
Angular js
Knoldus Inc.
 
introduction to Angularjs basics
introduction to Angularjs basicsintroduction to Angularjs basics
introduction to Angularjs basics
Ravindra K
 
Angularjs
AngularjsAngularjs
Angularjs
Sabin Tamrakar
 
Front end development with Angular JS
Front end development with Angular JSFront end development with Angular JS
Front end development with Angular JS
Bipin
 
Intoduction to Angularjs
Intoduction to AngularjsIntoduction to Angularjs
Intoduction to Angularjs
Gaurav Agrawal
 
Introduction of angular js
Introduction of angular jsIntroduction of angular js
Introduction of angular js
Tamer Solieman
 
Angular js
Angular jsAngular js
Angular js
Hritesh Saha
 
Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2
Knoldus Inc.
 

Similar to Workshop 14: AngularJS Parte III (20)

Angular js-crash-course
Angular js-crash-courseAngular js-crash-course
Angular js-crash-course
 
AngularJS in practice
AngularJS in practiceAngularJS in practice
AngularJS in practice
 
Angular workshop - Full Development Guide
Angular workshop - Full Development GuideAngular workshop - Full Development Guide
Angular workshop - Full Development Guide
 
Angular meetup 2 2019-08-29
Angular meetup 2   2019-08-29Angular meetup 2   2019-08-29
Angular meetup 2 2019-08-29
 
Angular Intermediate
Angular IntermediateAngular Intermediate
Angular Intermediate
 
AngularJs presentation
AngularJs presentation AngularJs presentation
AngularJs presentation
 
Angular js
Angular jsAngular js
Angular js
 
AngularJS Workshop
AngularJS WorkshopAngularJS Workshop
AngularJS Workshop
 
Intro to AngularJs
Intro to AngularJsIntro to AngularJs
Intro to AngularJs
 
Angularjs 131211063348-phpapp01
Angularjs 131211063348-phpapp01Angularjs 131211063348-phpapp01
Angularjs 131211063348-phpapp01
 
AngularJs
AngularJsAngularJs
AngularJs
 
Introduction to AngularJs
Introduction to AngularJsIntroduction to AngularJs
Introduction to AngularJs
 
Angular js
Angular jsAngular js
Angular js
 
introduction to Angularjs basics
introduction to Angularjs basicsintroduction to Angularjs basics
introduction to Angularjs basics
 
Angularjs
AngularjsAngularjs
Angularjs
 
Front end development with Angular JS
Front end development with Angular JSFront end development with Angular JS
Front end development with Angular JS
 
Intoduction to Angularjs
Intoduction to AngularjsIntoduction to Angularjs
Intoduction to Angularjs
 
Introduction of angular js
Introduction of angular jsIntroduction of angular js
Introduction of angular js
 
Angular js
Angular jsAngular js
Angular js
 
Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2
 

More from Visual Engineering

Workshop iOS 4: Closures, generics & operators
Workshop iOS 4: Closures, generics & operatorsWorkshop iOS 4: Closures, generics & operators
Workshop iOS 4: Closures, generics & operators
Visual Engineering
 
Workshop iOS 3: Testing, protocolos y extensiones
Workshop iOS 3: Testing, protocolos y extensionesWorkshop iOS 3: Testing, protocolos y extensiones
Workshop iOS 3: Testing, protocolos y extensiones
Visual Engineering
 
Workshop iOS 2: Swift - Structures
Workshop iOS 2: Swift - StructuresWorkshop iOS 2: Swift - Structures
Workshop iOS 2: Swift - Structures
Visual Engineering
 
Workhop iOS 1: Fundamentos de Swift
Workhop iOS 1: Fundamentos de SwiftWorkhop iOS 1: Fundamentos de Swift
Workhop iOS 1: Fundamentos de Swift
Visual Engineering
 
Workshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux AdvancedWorkshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux Advanced
Visual Engineering
 
Workshop 21: React Router
Workshop 21: React RouterWorkshop 21: React Router
Workshop 21: React Router
Visual Engineering
 
Workshop 18: CSS Animations & cool effects
Workshop 18: CSS Animations & cool effectsWorkshop 18: CSS Animations & cool effects
Workshop 18: CSS Animations & cool effects
Visual Engineering
 
Workshop 16: EmberJS Parte I
Workshop 16: EmberJS Parte IWorkshop 16: EmberJS Parte I
Workshop 16: EmberJS Parte I
Visual Engineering
 
Workshop 15: Ionic framework
Workshop 15: Ionic frameworkWorkshop 15: Ionic framework
Workshop 15: Ionic framework
Visual Engineering
 
Workshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJSWorkshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJS
Visual Engineering
 
Workshop 11: Trendy web designs & prototyping
Workshop 11: Trendy web designs & prototypingWorkshop 11: Trendy web designs & prototyping
Workshop 11: Trendy web designs & prototyping
Visual Engineering
 
Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6
Visual Engineering
 
Workshop 9: BackboneJS y patrones MVC
Workshop 9: BackboneJS y patrones MVCWorkshop 9: BackboneJS y patrones MVC
Workshop 9: BackboneJS y patrones MVC
Visual Engineering
 
Workshop 7: Single Page Applications
Workshop 7: Single Page ApplicationsWorkshop 7: Single Page Applications
Workshop 7: Single Page Applications
Visual Engineering
 
Workshop 6: Designer tools
Workshop 6: Designer toolsWorkshop 6: Designer tools
Workshop 6: Designer tools
Visual Engineering
 
Workshop 5: JavaScript testing
Workshop 5: JavaScript testingWorkshop 5: JavaScript testing
Workshop 5: JavaScript testing
Visual Engineering
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.
Visual Engineering
 

More from Visual Engineering (17)

Workshop iOS 4: Closures, generics & operators
Workshop iOS 4: Closures, generics & operatorsWorkshop iOS 4: Closures, generics & operators
Workshop iOS 4: Closures, generics & operators
 
Workshop iOS 3: Testing, protocolos y extensiones
Workshop iOS 3: Testing, protocolos y extensionesWorkshop iOS 3: Testing, protocolos y extensiones
Workshop iOS 3: Testing, protocolos y extensiones
 
Workshop iOS 2: Swift - Structures
Workshop iOS 2: Swift - StructuresWorkshop iOS 2: Swift - Structures
Workshop iOS 2: Swift - Structures
 
Workhop iOS 1: Fundamentos de Swift
Workhop iOS 1: Fundamentos de SwiftWorkhop iOS 1: Fundamentos de Swift
Workhop iOS 1: Fundamentos de Swift
 
Workshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux AdvancedWorkshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux Advanced
 
Workshop 21: React Router
Workshop 21: React RouterWorkshop 21: React Router
Workshop 21: React Router
 
Workshop 18: CSS Animations & cool effects
Workshop 18: CSS Animations & cool effectsWorkshop 18: CSS Animations & cool effects
Workshop 18: CSS Animations & cool effects
 
Workshop 16: EmberJS Parte I
Workshop 16: EmberJS Parte IWorkshop 16: EmberJS Parte I
Workshop 16: EmberJS Parte I
 
Workshop 15: Ionic framework
Workshop 15: Ionic frameworkWorkshop 15: Ionic framework
Workshop 15: Ionic framework
 
Workshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJSWorkshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJS
 
Workshop 11: Trendy web designs & prototyping
Workshop 11: Trendy web designs & prototypingWorkshop 11: Trendy web designs & prototyping
Workshop 11: Trendy web designs & prototyping
 
Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6
 
Workshop 9: BackboneJS y patrones MVC
Workshop 9: BackboneJS y patrones MVCWorkshop 9: BackboneJS y patrones MVC
Workshop 9: BackboneJS y patrones MVC
 
Workshop 7: Single Page Applications
Workshop 7: Single Page ApplicationsWorkshop 7: Single Page Applications
Workshop 7: Single Page Applications
 
Workshop 6: Designer tools
Workshop 6: Designer toolsWorkshop 6: Designer tools
Workshop 6: Designer tools
 
Workshop 5: JavaScript testing
Workshop 5: JavaScript testingWorkshop 5: JavaScript testing
Workshop 5: JavaScript testing
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.
 

Recently uploaded

Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
KrzysztofKkol1
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
Jelle | Nordend
 
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Hivelance Technology
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
XfilesPro
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
Sharepoint Designs
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 

Recently uploaded (20)

Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 

Workshop 14: AngularJS Parte III

  • 1. Front End Workshops XII. AngularJS - Part 3 Juan Luís Marí jlmari@visual-engin.com Pablo Balduz pbalduz@visual-engin.com Héctor Canto hcanto@visual-engin.com
  • 3. In previous chapters... ● AngularJS intro ● Modules and dependency injection ● Filters, Controllers and Services ● Routes ● Digest cycle ● Data binding ● Directives ...
  • 4. Overview ● Scope functions ● Patterns ● Testing ● Directives functions (continuation of custom directives) ○ compile, controller and link functions ● Build-in directives
  • 5. Scope functions — Watch, apply & Digest—
  • 6. Scope functions Watch, apply and digest are $scope functions used to monitor and process binded data in terms of the digest cycle. There are used very scarcely but it is good to know in order to understand bad updates, performance issues and digest loop errors. http://tutorials.jenkov.com/angularjs/watch-digest-apply.html
  • 7. Scope functions - watch Data binding creates ‘watchers’ internally. Every digest cycle will check watched variables for changes and react accordingly. Variables are watched implicitly when we use double curly brackets: {{myVar}} and explicitly: $scope.watch(‘myVar’, function(){...}); As 1st parameter we can call a function, also. The 2nd function is called a ‘listener’. http://tutorials.jenkov.com/angularjs/watch-digest-apply.html
  • 8. Scope functions - apply Through the digest cycle, watched variables will be monitored. But sometimes we don’t have a variable to watch and we are waiting for an async event. Some directives and services already use apply for us internally. ● ng-click ● $timeout ● $http and explicitly: http://jimhoskins.com/2012/12/17/angularjs-and-apply.html
  • 9. Scope functions - apply function Ctrl($scope) { $scope.message = "Waiting 2000ms for update"; setTimeout(function () { $scope.$apply(function () { $scope.message = "Timeout called!"; }); }, 2000); } Note: Instead, you could use $timeout.
  • 10. Scope functions - apply When to use apply: ● Very rarely ( or better NEVER ). Two well-know cases: ○ dealing with sockets ○ wrapping non-Angular code ○ when you know a variable is initialized outside the digest cycle How to use apply: ● with a function preferably: $scope.apply( function() {}); ● Tip: Use $timeout without delay (but it will be defered). http://stackoverflow.com/questions/23070822/angular-scope-apply-vs-timeout-as-a-safe-apply
  • 11. Scope functions - digest $scope.digest triggers a digest cycle, but we should never call it directly. ● digest is triggered internally ● after the execution of $scope.apply, at the end of its execution We use apply (and therefore digest) when we don’t have a variable to watch and we are waiting for an async event. http://jimhoskins.com/2012/12/17/angularjs-and-apply.html
  • 12. Common Patterns — non trivial only—
  • 13. Known patterns Angular is already build as a collection of patterns ● Services are singletons. ● Factories are factories. ● Data-binding and $scope.watch implements a watcher pattern. ● Controller and Template. ● Dependency injections: everywhere. http://blog.mgechev.com/2014/05/08/angularjs-in-patterns-part-1-overview-of-angularjs/
  • 14. Observer (publish-subscribe) Observers are similar to watchers, but instead of permanently watch a fuction they subscribe to events. To subscribe: $scope.on(‘event-name’, function handler() { //your code here } And to launch the event: function ExampleCtrl($scope) { $scope.$emit('event-name', { foo: 'bar' }); //upwards on the scope hierarchy $scope.$broadcast('event-name', { foo: 'bar' }); //downwards to all child $rootScope.$broadcast('event-name', { foo: 'bar' }); //to everybody } http://blog.mgechev.com/2014/07/05/angularjs-in-patterns-part-3/
  • 15. Event listener (View observer) Multiple build-in directives are event listeners: ● ng-click, ng-dbl-click ● ng-mouse… ● ng-keydown, ng-keyup ● ng-change You can add your own event listeners. Let see an example: http://tutorials.jenkov.com/angularjs/events.html
  • 16. Event listener example angular.module('myApp', []) .directive('myDirective', function myDirective () { return { restrict: 'AE', link: myDirectiveLink } }); function myDirectiveLink (scope, element, attrs) { var domElement = element[0]; // Get the native JS element domElement.addEventListener("dragstart", myEventListener, false); // You can use ngDraggable but it uses jQuery } function myEventListener () { // Handle event here }
  • 17. Other patterns Proxy ● a service using $http or $resource (Restful http) internally. Data Mapper ● a service returning Model instances calling $http or $resource.
  • 19. AngularJS takes well care of testing: ● Well supported by Karma (test runner) ● Can use Mocha, Sinon, Chai ... ● Protractor and ngMock specially build for testing Angular. ● KEY: inject dependencies and mock key elements Testing in AngularJS https://www.smashingmagazine.com/2014/10/introduction-to-unit-testing-in-angularjs/ https://quickleft.com/blog/angularjs-unit-testing-for-real-though/
  • 20. E2E framework based on Jasmine and Selenium’s WebDriver Ability to simulate user interaction Simulation in selenium and most browsers Usually, we will run tests in Selenium (without UI) and all target browsers. We can automate this with Gulp Protractor
  • 21. Provides several services to mock Angular behaviour: ● $timeout, ● $controller : inject controllers ● $httpBackend: patch or mock $http calls ● module() and inject(): ○ resolve dependencies on tests ○ patch methods ○ mock scope ngMock http://www.bradoncode.com/blog/2015/05/27/ngmock-fundamentals-angularjs-testing-inject/
  • 22. Unit Testing ● Testing individual, small units of code → Need of isolated code ○ Note: Bad isolated code may force the app to create related pieces as server’s requests or new DOM elements → Affect other tests and produce errors ● Solutions: Dependency Injection and Mocking Services ○ DOM elements abstracted → never directly modified ○ XHR requests and petitions → simulated (by dependency injection of $httpBackend) ■ All we really need is to verify whether a certain request has been sent or not, or alternatively just let the application make requests, respond with pre-trained responses and assert that the end result is what we expect it to be.
  • 23. Dependency Injection ● AngularJS built-in. Allows to pass in a component's dependencies and stub or mock them as you wish. ● Does not modify any global variable, so it does not affect other tests angular.module('app', []) .controller(ExampleCtrl, function ExampleCtrl($scope) { ... }); describe('ExampleCtrl tests', function() { beforeEach(module('app')); var $controller; beforeEach(inject(function(_$controller_){ // The injector unwraps the underscores (_) from around the parameter names when matching $controller = _$controller_; })); describe('block to be tested', function() { it('test1', function() { // create a scope object for us to use. var $scope = {}; // now run that scope through the controller function, injecting any services or other injectables we need. // **NOTE**: this is the only time the controller function will be run, so anything that occurs inside of that will already be done before the first spec. var controller = $controller('ExampleController', { $scope: $scope }); }; }); });
  • 24. $httpBackend ● angular-mocks module allows to inject and mock the AngularJS services, so they can be extended and used synchronously → $httpBackend is one of them ● Service in module ngMock ● Fake HTTP backend implementation suitable for unit testing applications that use the $http service. ● Allows not using external dependencies, so the requests to URLs are mocked. ● With dependency injection, it is easy to inject $httpBackend mock and use it to verify the requests and respond with some testing data without sending a request to a real server. ● $httpBackend.flush() allows to flush pending requests, so the test will run synchronous but preserving the async of the backend API
  • 25. Custom directives - Continuation — DOM Manipulation—
  • 26. There are 3 types of functions, by order of execution: ○ compile, controller and link ● Compile happens once, before the template is compiled. ● The rest of functions is run once for each time the directive is used ■ For example in a ng-repeat of 4 elements, 4 loops ○ Controller initialize the scope. ○ Link happens when the linking is being made, by default after. ○ We can divide link into two, pre-link and post-link ■ Pre-link happens before the element is linked to the scope ■ Post-link happens just after, when the element affected is on the DOM. ● This is the most usual and potentially safest Custom directives: functions http://www.undefinednull.com/2014/07/07/practical-guide-to-prelink-postlink-and-controller-methods-of-angular-directives/
  • 27. compile: function CompilingFunction($templateElement, $templateAttributes) { …} link: function LinkingFunction($scope, $element, $attributes) { ... } …} link: { pre: function PreLinkingFunction($scope, $element, $attributes) { ... }, post: function PostLinkingFunction($scope, $element, $attributes) { ... }, } Custom directives: functions http://plnkr.co/edit/qrDMJBlnwdNlfBqEEXL2?p=preview https://github.com/angular/angular.js/wiki/Understanding-Directives
  • 28. Custom directives: link, prelink, postlink ● There are 4 arguments available for these functions (in this order) ○ scope, elements, attributes and controllers ● You can access the DOM, you have the element. ● By default use link directly, which is equivalent to post-link alone. ● Remember, if possible provide values as soon as you can. ○ Don’t wait to post-link, do it in the controller or in compile ● [post-]link is the View part where you have everything in place and you do the last adjustments and decisions regarding the DOM.
  • 29. Custom directives: link, prelink, postlink var app = angular.module('app', []); app.directive('dad', function () { return { restrict: 'EA', template: '<div class="dad">{{greeting}}{{name}}'+ '<son></son>'+ '</div>', link: { pre: function(scope,elem,attr){ scope.name = 'Paul'; scope.greeting = 'Hey, I am '; } } }; }) app.directive('son', function () { return { restrict: 'EA', template: '<div class="son">{{sonSays}}</div>', link: function(scope,elem,attr){ scope.sonSays = 'Hey, I am David, and my dad is '+ scope.name; } }; }); <div ng-app="app"> <dad></dad> </div> Hey, I am Paul Hey, I am David, and my dad is Paul
  • 30. Custom directives: post-link, ● It is safe to manipulate the DOM in post-link as the element is already in the DOM. ● It is possible to access the scope ● All child directives are linked so it’s safe to access them ○ their scope and the elements they affect. ● It is safe to attach events handlers to elements.
  • 31. Custom directives: pre-link, ● Use of pre-link is scarce, ○ A child needs data from its parent ● Safe to attach an event to the DOM element ○ Not safe to access DOM elements from child directives ● The scope is not linked yet.
  • 32. Custom directives: compile ● In this phase AngularJS manipulates the DOM of the HTML template ● Each directive has a chance to do some processing for all and each DOM nodes it appears in. ● The scope is not attached yet. ● The template is still bare, without binding nor substitutions.
  • 34. Built-in directives ● ng-model ● ng-src ● ng-class ● ng-style ● ng-click ● ng-if ● ng-show ● ng-include ● ng-repeat
  • 35. ng-model ● Binds inputs to scope properties ● If property does not exist, it is created and added to the scope ● Watches model by reference Important! ● Allows animation and validation <script> angular.module("module", []) .controller("controller", ['$scope', function($scope) { $scope.value = "Luis"; }]); </script> <div ng-controller="controller"> Name: <input ng-model="value"/> </div>
  • 36. ng-src ● src leads to problems when evaluating expressions ● Prevents browser from loading resources before an expression is evaluated Buggy way Correct way <img src="http://www.gravatar.com/avatar/{{hash}}" alt="Description"/> <img ng-src="http://www.gravatar.com/avatar/{{hash}}" alt="Description" />
  • 37. ng-class ● Allows to dynamically set CSS classes on an HTML ● Ways of operating (syntax): ○ String ○ Key-value pair object ○ Array of strings (type 1) or objects (type 2) ○ Ternary operator ● Usage options: class and attribute
  • 38. ng-class String syntax Array syntax Key-value pair syntax <input type="text" ng-model="style"> <div ng-class="style">String syntax</div> <input type="text" ng-model="styleOne"> <input type="text" ng-model="styleTwo"> <div ng-class="[styleOne, styleTwo]">Array syntax</div> <input type="checkbox" ng-model="great"> Barça <input type="checkbox" ng-model="poor"> Madrid <div ng-class="{ styleOne: great, styleTwo: poor }">
  • 39. ng-class Ternary operator Specific numbers ngClass as a class ngClass as an attribute (every example shown except the last one) ng-class="variableToEvaluate ? 'class-if-true' : 'class-if-false'"> <ul><li ng-class="{ 'text-success': $first }" ng-repeat="item in items">{{ item.name }}</li></ul> <div class="item ng-class:type;">Stuff</div> <div class="item ng-class:[styleOne, styleTwo];">Stuff</div> <div class="item ng-class:{ 'text-error': wrong };">Stuff</div>
  • 40. ng-style ● Allows setting style on an HTML conditionally ● Interpolates javascript object into style attribute, not css class Following directive will be translated to style=”color:red” Following directive will be translated to class=”color” For interpolation, instead of doing this: do this: ng-style="{color: 'red'}" ng-class="{color: colorClass}" style="width: {{progress}}" ng-style="{width: progress}"
  • 41. ng-click ● Specify custom behavior when an element is clicked <button ng-click="count = 0"> Reset </button> <button ng-click="reset()"> Reset </button>
  • 42. ng-show ● Shows / hides HTML element based on an expression ● Adds / removes ng-hide class ● Animations Element visible when $scope.value is truthy Element hidden when $scope.value is falsy <div ng-show="value"></div> <div ng-show="value" class="ng-hide"></div>
  • 43. ng-if ● Removes / recreates part of the DOM ● scope is removed and recreated ● ng-model with ng-if issues ● Animations Similar usage to ngShow / ngHide <div ng-if="value"></div>
  • 44. ngAnimate ● Include angular-animate.js and load module ● Directives support ○ ngRepeat ○ ngShow ○ ngHide ○ ngIf ○ ... ● CSS / JS based animations
  • 45. ngAnimate CSS based animations ● No need of JavaScript code at all ● CSS class referenced between HTML and CSS ● 2 css classes added depending on the animation event <div ng-if="bool" class="fade"> Fade me in out </div> <button ng-click="bool=true">Fade In!</button> <button ng-click="bool=false">Fade Out!</button> .fade.ng-enter { transition:0.5s linear all; opacity:0; } .fade.ng-enter.ng-enter-active { opacity:1; }
  • 46. ngAnimate CSS based animations ● Staggering animations ● ng-Animate css class ● Custom keyframe animations .zipper.ng-animate { transition:0.5s linear all; } .zipper.ng-enter { opacity:0; } .zipper.ng-enter.ng-enter-active { opacity:1; } .zipper.ng-leave { opacity:1; } .zipper.ng-leave.ng-leave-active { opacity:0; }
  • 47. ngAnimate JS based animations Register JavaScript animation on the module <div ng-repeat="item in items" class="slide"> {{ item }} </div> myModule.animation('.slide', [function() { return { enter: function(element, doneFn) { // Do some cool animation and call doneFn }, move: function(element, doneFn) {}, leave: function(element, doneFn) {} } }]);
  • 49. ng-include ● Add HTML code from external file to the current one ○ Fetches, compiles and includes an external HTML fragment ● Create new scope for the included template ● The included HTML files can also contain AngularJS code <div ng-include="'myFile.html'"></div>
  • 50. ng-include ● Cross-domain: ○ By default, the ng-include directive does not allow you to include files from other domains. ○ To include files from another domain, you can add a whitelist of legal files and/or domains in the config function of your application: var app = angular.module('myApp', []) app.config(function($sceDelegateProvider) { $sceDelegateProvider.resourceUrlWhitelist([ 'http://www.refsnesdata.no/**' ]); });
  • 51. ng-repeat ● Instantiates a template once per item from a collection <div ng-repeat="item in collection"> ... </div> ● Each template instance gets its own scope ● It is also possible to get ngRepeat to iterate over the properties of an object ○ Note: The built-in filters orderBy and filter do not work with objects <div ng-repeat="(key, value) in myObj"> ... </div>
  • 53. ng-repeat ● Continuously watching if changes in the collection happens ○ Automatically changing DOM (when adding, removing or reordering items) ○ “Keep track” items with their DOM elements → Avoids re-render them if not needed ● Default tracking function (changeable with track-by) ○ Tracks by identity of item ○ Does not allows duplicated items ● ng-repeat in more than one parent element of the HTML ○ ng-repeat-start ○ ng-repeat-end range of the repeater