SlideShare a Scribd company logo
TechTalk | 3 February 2014
Spyros Ioakeimidis
AngularJS

❖

Seemed a bit weird in the beginning

❖

All these new terms and concepts…

!2
Modul
es

Depen
dency
Injecti
on
Provid

ws
Vie

ers
Promi
ses
Servic

es

Mo

s
er
ilt

F

els
d
cto
Fa

Contro

ies
r

Direct
ives

llers

!3
AngularJS
❖

Made for SPA (Single Page Applications)

❖

It has a learning curve!

❖

Maintained by Google (for some this is good, for
others not)

❖

Community keeps increasing

!4
AngularJS
❖

MVC principle

❖

Others say it is an SPA framework

❖

Others that it is just a library to help you built a
framework

❖

AngularJS is a toolset (extensibility)

!5
!6
Modules

!7
Modules
❖

app = angular.module(‘myApp’, [])
❖
❖

It is required when creating a new module
(module declaration)

❖

❖

Use of an array to include other modules

app = angular.module(‘myApp’) retrieves
an existing module (module reference)

Modules cannot be included at runtime
!8
Modules
❖

We have a module app
❖

.value()
❖

❖

global values, good for defaults

.constant()
❖

same as Value, but the it is expected to not change

❖

.service()

❖

.factory()

❖

.provider()
!9
MVC

(or whatever…)

!10
W(M)VC
❖

Whatever (Model)
❖
❖

❖

It is the $scope, which contains models and functions as well
e.g. $scope.event = {‘title’: ‘Placed Event’}

View
❖

❖

HTML markup

Controller
❖

An JS object

❖

app.controller(‘MyController’, function{})

❖

DOM manipulation does not belong to controllers
!11
Whatever
(Data)
stored in $scope of

contains

View
(HTML)

communicates with

Controller
(Business Logic)

provides
functionality

Hiller, Christopher. “Developing an AngularJS Edge.”
!12

Directive
(DSL in HTML)
provides functionality

Service
(Library)
Scope.models

!13
Scope
❖

prototypical inheritance

❖

treat scope as read-only in templates

❖

when binding to templates ng-model=“user.name”, always
use a dot

❖

$rootScope

❖

$scope.$apply()
❖

$eval() -> $exceptionHandler(e) -> $digest()

❖

e.g. when observing DOM elements directly in directives
!14
Scope
❖

$scope.$watch(‘foo’, function() {})
❖

observe a model for changes, evaluates by reference

❖

will fire when $scope.foo = ‘’ and then $scope.foo =
‘bar’

❖

but not when $scope.foo.bar = ‘’ and then
$scope.foo.bar = ‘baz’

❖

can evaluate by value (expensive operation!)

❖

when writing own $watch, it must be fast and it must be
idempotent
!15
Scope
❖

$scope.$digest()

❖

$scope.$eval()

❖

$scope.{$on, $broadcast, $emit}

!16
$emit(‘eventB’, …)

$emit(‘eventC’, …)

Controller A

$emit(‘eventB’, …)

$on(‘eventA’, …)
$rootScope.$on(‘eventC’, …)

$on(‘eventB’, …)
$on(‘eventC’, …)

$broadcast(‘eventA’, …)

Controller B

Controller C

$broadcast(‘eventA’, …)

!17

$emit(‘eventC’, …)

$on(‘eventA’, …)
Model
❖

Scope is not the model!

❖

$scope.foo, where the foo is the model

❖

data-binding, <p>{{foo.bar}}</p>
❖

a portion of view is bind to a model

❖

when either one changes, they are both
synchronized
!18
View

!19
View
❖

presentation logic

❖

does not know anything about the controller, nor
the controller about it

❖

everything goes through the Scope

❖

integrates with models, directives, filters

!20
View
❖

ng-repeat

❖

this would not work




<div ng-repeat=“number in [1, 2, 3]”>

<input type=“text” ng-model=“number” />

</div>



but this would work




<div ng-repeat=“number in [{n: 1}, {n: 2}, {n: 3}]”>

<input type=“text” ng-model="number.n"/>

</div>
❖

$index, $first, $middle, $last, $event, $parent

❖

ng-click, ng-show, ng-model, ng-change, ng-href …
!21
View
❖

orderBy, filter

❖

<div id=“{{post.id}}”

ng-repeat=“post in posts|

orderBy:order:descending|

filter:filter”>

</div>

❖

in Controller

$scope.order = ‘date’

$scope.descending = true

❖

and filter would come from an text box

❖

ngClassEvent, ngClassOdd
!22
View
❖

compilation for templates and markup is done in
two steps
Step 1: compile

Step 2: link

var linker = $compile(template)

var compiled = linker(scope)

1) template examined for directives
1) scope into the link function of its directive
2) AngularJS collects them and 

2) get back AngularJS element representing

produces a link function
the DOM fragment
* no data has been inserted in the template
$compile(template)(scope)
!23
Controller

!24
Controller
❖

they are for the business logic

❖

they are not meant to interact with the DOM

❖

services can be injected to share common
functionality with other controllers

!25
Dependency Injection
❖

A nice way to create modular applications

❖

it is magic (but not really…)
❖

Angular converts functions to strings .toString()

❖

runs string matching against them

❖

determines from the name of the parameter what
you want to inject


!26
Directives

!27
Directives
❖

Create your own DSL

❖

A nice way to include logic in the views

❖

this is where DOM manipulation should happen!

❖

Can have an isolated scope

❖

Defines a linking function
❖

❖
❖

is passed a Scope object, an element, and the element’s attributes
(optionally a controller inst.)
where the logic goes

Need a scope, use link. Don’t? Use compile.
!28
Directives
HTML with
HTML with
HTML with
directives
directives
directives

examines

<my-directive>

finds

compile()

returns

Scope
object

link
(scope)

Hiller, Christopher. “Developing an AngularJS Edge.”
!29

returns

Compiled
template with
data
Directives
app.directive(‘new-event’, function() {

return {

restrict: ‘E’,

scope: {

event: ‘=’,

linked to the parent scope
save: ‘=’,

cancel: ‘=’,

saveText: ‘@’,

copied verbatim
cancelText: ‘@’

},

replace: true,

template: 

‘<div ng-show="event">' +

‘<input type=“text” ng-model=“event.temp.title”/><br/>’ +

‘<input type=“text” ng-model=“event.temp.subtitle”></../>’ +

‘<button ng-click=“cancel(event)”>{{cancelText}}</button>’ +

‘<button ng-click=“save(event)”>{{saveText}}</button></div>’

};

});

}

}

!30
Directives
<new-event

event=“event”

cancel=“cancel”

save=“save”

cancel-text=“Cancel”

save-text=“Save”>

</new-event>

!31

linked to the parent scope

copied verbatim
Directives
app.directive(‘event-info', function() {

return {

restrict: ‘E’,

replace: true,

templateUrl: ‘views/eventInfo.html’

transclude: true,

scope: {

startDate: ‘=’,
endDate: ‘=’

},

link: function(scope, element) {
infoButton = element.children()[0].children[0].children[0]

scope.open = false;

angular.element(infoButton).bind(‘click’, function() {

scope.$apply(‘open = !open’);

});

};

};

});
!32
Directives
<div class="event-info">

<div class="event-info-content">

<div class="event-title" ng-transclude>

<span class=“addtional-info-button”>More...</span>

</div>

<div class="additional-info>" ng-show="open">

<div class=“event-start">{{startDate}}</div>

<div class="event-end">{{endDate}}</div>

</div>

</div>

</div>

We could call it like:
<event-info start-date=“event.event_start" end-date="event.event_end">

{{event.title.en}}

</event-info>

!33
Directives
❖

‘=foo’ links directly to parent scope

❖

‘@foo’ assigns an attribute to whatever the key is

❖

‘&foo’ binds expression and executes it against the parent
scope
❖
❖

<my-directive on-close=“hide()”></my-directive>

❖
❖

in directive definition: scope: {‘close’: ‘&onClose’}

in directive’s template: ng-click=“close()”

There are also ‘A’, ‘AE’, ‘C’, and ‘M’ restrict options
!34
Directives
❖

write reusable and generic directives

❖

the less reusable (involving scopes) the less
maintainable they become

❖

write directives with isolated scopes!

❖

share functionality between directives using a
controller

❖

long learning curve, but powerful!
!35
Service | Factory | Provider

!36
Service | Factory | Provider
❖

the backbone of the an AngularJS app

❖

keep the controller from thinking too hard

❖

… and directives have only one function, make it
readable and testable (inject services)

!37
Service | Factory | Provider
Needs a $get Explicitly returns Present during
Component Instantiated
method
something
$config()

Service

Yes

No

No

No

Factory

No

No

Yes

No

Provider

Yes

Yes

No

Yes

Hiller, Christopher. “Developing an AngularJS Edge.”
!38
Service
❖

it is a singleton, instantiated the first time is
requested

❖

requires less memory and processing power than
the others

!39
Promises
EventsCtrl
(Controller)
1) Calls getEvents()

Promise
3) Returns a
Promise

Events
(Service)

!40

Server
2) Asks Server
for a list of Events
Promises
4) Returns a list
of events

Server

Events
(Service)
5) Promise resolved
with events data

EventsCtrl
(Controller)

Promise
6) Takes action with
data received from
resolved promise

!41
Factory

❖

it can return a function, object, or primitive

❖

useful for utils

!42
Provider
❖

can be configured during initialization

❖

$http is also a provider
injects

config()

$httpProvider

injects

run()

$http

!43
Provider
Configure with Plugin B in
Module.config()

Plugin A
DialogProvider
Plugin B

Dialog
Uses plugin B at runtime

Hiller, Christopher. “Developing an AngularJS Edge.”
!44
Filters

!45
Filters
❖

execute logic against models only the view cares about

❖

app.filter(‘product’, function() {

return function(a, b, c) {

return a * b * c;

};

});

❖

can be used like:




<span>the product is {{a|product:b:c}}</span>
❖

executed often, so keep them simple

❖

they should not have side-effects
!46
More…
❖

History

❖

Routing

❖

Compiler

❖

Injector

❖

Template

!47
Testing (intro)
❖

Jasmine (behavior-driven testing)

❖

aware of asynchronous events

❖

take advantage of binding information

❖

excellent mock objects ($log)

!48
ng-modules
❖

http://ngmodules.org/, currently 440 modules (++)

❖

angular-ui, bootstrap, ui-router

❖

restangular

❖

angular-translate

❖

angular-cache

❖

…
!49
built.with.angularjs
❖

http://builtwith.angularjs.org/

❖

http://www.google.com/doubleclick/

Create, transact, and manage digital advertising

❖

http://demo.songpane.com/

Form song set lists (songs, lyrics, chords)

❖

http://www.zaptravel.com/

Deals for experiences of interest

❖

http://hat.jit.su/

Distributed scrum planning poker

❖

…
!50
References
❖

Hiller, Christopher. “Developing an AngularJS
Edge.”

❖

Mastering Web Application Development with
AngularJS

❖

Ari Lerner “ng-book - The Complete Book on
AngularJS”

!51
AngularJS - TechTalk 3/2/2014
AAAAngularosum!

More Related Content

ODP
Angularjs
PDF
Seven Versions of One Web Application
PPT
Angular 8
PPTX
Angular js 2
PDF
Overview of the AngularJS framework
PDF
Building scalable applications with angular js
PDF
Workshop 26: React Native - The Native Side
PDF
Intro to JavaScript
Angularjs
Seven Versions of One Web Application
Angular 8
Angular js 2
Overview of the AngularJS framework
Building scalable applications with angular js
Workshop 26: React Native - The Native Side
Intro to JavaScript

What's hot (20)

PDF
AngularJS Project Setup step-by- step guide - RapidValue Solutions
PPTX
Angular modules in depth
PDF
Workshop 24: React Native Introduction
PDF
A gently introduction to AngularJS
PPTX
Angular2 + rxjs
PPTX
Angular2 for Beginners
PDF
Exploring Angular 2 - Episode 2
PDF
Tech Webinar: Angular 2, Introduction to a new framework
PPTX
Angular js
PDF
Angular2 Development for Java developers
PDF
AngularJS Basics and Best Practices - CC FE &UX
PDF
Exploring Angular 2 - Episode 1
PDF
Data Flow Patterns in Angular 2 - Sebastian Müller
PPTX
Single Page Applications with AngularJS 2.0
PDF
Dart for Java Developers
PPTX
Angular Workshop_Sarajevo2
PPTX
Angular tutorial
PPTX
The road to Ember.js 2.0
PDF
Workshop 25: React Native - Components
PDF
Angular 2 Essential Training
AngularJS Project Setup step-by- step guide - RapidValue Solutions
Angular modules in depth
Workshop 24: React Native Introduction
A gently introduction to AngularJS
Angular2 + rxjs
Angular2 for Beginners
Exploring Angular 2 - Episode 2
Tech Webinar: Angular 2, Introduction to a new framework
Angular js
Angular2 Development for Java developers
AngularJS Basics and Best Practices - CC FE &UX
Exploring Angular 2 - Episode 1
Data Flow Patterns in Angular 2 - Sebastian Müller
Single Page Applications with AngularJS 2.0
Dart for Java Developers
Angular Workshop_Sarajevo2
Angular tutorial
The road to Ember.js 2.0
Workshop 25: React Native - Components
Angular 2 Essential Training
Ad

Viewers also liked (11)

PDF
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
PPTX
BDOTNET AngularJs Directives - Uday
PDF
AngularJs
PDF
Dive into AngularJS and directives
PDF
Reative UI
PDF
ReactiveUI: Rx + MVVM
PPTX
AngularJS Directives
PPTX
AngularJS custom directive
PPTX
Building AngularJS Custom Directives
PDF
Angular 2 observables
PPTX
Angular2 rxjs
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
BDOTNET AngularJs Directives - Uday
AngularJs
Dive into AngularJS and directives
Reative UI
ReactiveUI: Rx + MVVM
AngularJS Directives
AngularJS custom directive
Building AngularJS Custom Directives
Angular 2 observables
Angular2 rxjs
Ad

Similar to AngularJS - TechTalk 3/2/2014 (20)

PPTX
The AngularJS way
PDF
Angular basicschat
PDF
AngularJS Workshop
PPTX
Understanding angular js
PPTX
Angular workshop - Full Development Guide
PPTX
Angular js 1.0-fundamentals
PPTX
AngularJs presentation
PPTX
Intro to AngularJs
PPTX
Practical AngularJS
PDF
AngularJS: Overview & Key Features
PPTX
Learning AngularJS - Complete coverage of AngularJS features and concepts
ODP
Angular js-crash-course
ODP
AngularJs Crash Course
PPTX
Training On Angular Js
PDF
Everything You Need To Know About AngularJS
PPTX
AngularJS.part1
PPT
Coffee@DBG - Exploring Angular JS
PDF
"Angular.js Concepts in Depth" by Aleksandar Simović
PPTX
Angular Presentation
PPT
Introduction to AngularJS
The AngularJS way
Angular basicschat
AngularJS Workshop
Understanding angular js
Angular workshop - Full Development Guide
Angular js 1.0-fundamentals
AngularJs presentation
Intro to AngularJs
Practical AngularJS
AngularJS: Overview & Key Features
Learning AngularJS - Complete coverage of AngularJS features and concepts
Angular js-crash-course
AngularJs Crash Course
Training On Angular Js
Everything You Need To Know About AngularJS
AngularJS.part1
Coffee@DBG - Exploring Angular JS
"Angular.js Concepts in Depth" by Aleksandar Simović
Angular Presentation
Introduction to AngularJS

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Tartificialntelligence_presentation.pptx
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Getting Started with Data Integration: FME Form 101
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Approach and Philosophy of On baking technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Empathic Computing: Creating Shared Understanding
PPTX
1. Introduction to Computer Programming.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Encapsulation_ Review paper, used for researhc scholars
Reach Out and Touch Someone: Haptics and Empathic Computing
Tartificialntelligence_presentation.pptx
A comparative analysis of optical character recognition models for extracting...
Heart disease approach using modified random forest and particle swarm optimi...
Getting Started with Data Integration: FME Form 101
Digital-Transformation-Roadmap-for-Companies.pptx
SOPHOS-XG Firewall Administrator PPT.pptx
Approach and Philosophy of On baking technology
Diabetes mellitus diagnosis method based random forest with bat algorithm
MIND Revenue Release Quarter 2 2025 Press Release
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Empathic Computing: Creating Shared Understanding
1. Introduction to Computer Programming.pptx
A Presentation on Artificial Intelligence
Building Integrated photovoltaic BIPV_UPV.pdf

AngularJS - TechTalk 3/2/2014