SlideShare a Scribd company logo
AngularJS
A better way to create web apps
Agenda
O Introduction to AngularJS
O Some concepts used in AngularJS
O Testing application built with Angular
O Productivity and debugging tools
O Code organization for large projects
O Code demo as we go on
Why AngularJS
O HTML was created to transmit markup
data on web. E.g. <b> tags for making text
bold etc.
O JavaScript added for interactivity.
O And then comes AJAX and rich web
apps.
O And things get messed up by adding
listeners after getting ids, callbacks and
series of callbacks.
What can AngularJS do
O Less boilerplate code, faster development
with less code and thus less tests and
bugs.
O Clean separation of DOM
manipulation, business logic and views
make things easier to test.
O Declarative UI design means designers
can focus on their work without even
knowing JavaScript.
O Support for cool stuff like promises.
Angular components
This is the real stuff
Angular App
Modules
Models
Controllers
Templates (and Views)
Directives
Services
Filters
Routes
Module
O Angular apps have nothing like a main
method
O Directive ng-app used to bootstrap the
module
O E.g. <html ng-app=“someModule”>
O A module can contain several other
components
O var module =
angular.module(„someModule‟, [])
O Second argument is an array of other
modules which are requisites for the current
module.
Models
O In Angular, model is any data that is
reachable as property of $scope object
O Can also create models inside templates
using the ng-model directive e.g. <input
type=“email” ng-model=“user.email”>
O Can watch models for changes using
$watch(watchFn, watchAction, deepWatch)
O deepWatch is a boolean which allows to
watch attributes of the object returned by
watchFn
Controllers
O Set up the $scope
O Add behavior to $scope
O All business logic to be written in here.
O Can use the one controller per template
to create really traditional websites.
Templates
O Specify how things should show up and
what happens on particular user actions.
O Angular templates are not the view in
MVC. Compiled angular templates are the
views.
O Declaratively create the UI using
directives.
Directives
O One of the most important part of Angular
O Allows you to extend HTML to define
custom tags, attributes and classes for UI
components.
Jquery UI Angular Directives
<div id=“tabs”>
<ul>
<li><a href=“#tabs-1”> Tab1</a></li>
<li><a href=“#tabs-2”> Tab2</a></li>
</ul>
<div id=“#tabs-1>Content</div>
<div id=“#tabs-2>Content2</div>
</div>
<script>
$(function() {
$(„#tabs”).tabs();
});
</script>
<tab-set>
<tab title=“Tab1”>Content</tab>
<tab title=“Tab2”>Content2</tab>
</tab-set>
Now what if you need tabs on
10 pages in your whole
application ?
Services
O Stuff required at various places e.g. by
various controllers.
O E.g. a http request to get list of blog posts
in a blogging application would be
required while showing all blog posts, in
the admin edit/publish view etc.
O Three different ways to create services
i.e. services, factory and provider.
Filters
O Filters are transformations applies to
objects
O Not important to logic used to process
models e.g. currency sign (like $)
O E.g. lowerCase, sort etc.
O Built-in filters like json
O Custom filters can be defined with
app.filter(„filterName‟, function (obj)
{})
Routes
O Used to map templates and associated
controller to URLs.
O Supports HTML5 mode where you get
history support.
O That‟s it !
Form Validation
O Provides special support for two
properties $valid, $invalid.
O Can use them like <button ng-
disabled=“!formName.$valid”> to disable a
form until its all elements are correct.
O polyfills for things like „required‟ on non-
HTML browsers
Server-side communication
O $http wraps the XHR to provide
asynchronous requests.
O $resource for interacting with RESTful
resources. (provided as a separate
module in angular-resource.js)
O $http requests support promises (talk
about this later)
Secrets of all the magic in
Angular
What actually happens
O Template loads with all the angular directives
O Angular script loads and after template
loading finishes, it looks for ng-app to find
application boundaries.
O Compile phase – Angular walks the DOM to
identify registered directive and transforms
them.
O Link Phase – This runs a link function for
each directive which typically creates listeners
on DOM or model and keeps view and model
in sync .
$scope
O $scope allows binding and
communication between controller and
templates.
O All the model objects are stored on it.
O Makes sense not to store everything on it
as it would degrade performance.
Data binding
O Binding two sources together and keep
then updated of changes.
O Angular supports two-way data binding.
O Change in models get reflected
everywhere in view.
O Changes in view gets reflected in the
model.
Dependency Injection
O Follows Law of Demeter or principle of
least knowledge.
O Instead of creating things, just ask for
what is required.
O E.g. to create a Car you need an engine
so instead of calling an engine Factory in
the constructor, pass an engine as
parameter.
O Makes things easier to test (e.g. mock
$http), less code and easier to change
later.
Promises
O A promise is an interface that deals with
objects that are returned or get filled in at
a future point in time.
O Current approach with callbacks has
problems of indentation while chaining
multiple calls, losing errors reported
between callbacks unless manually
handled and doing real stuff in innermost
callback.
Promises
O In angular a promise gives with a then
function which takes two functions as a
parameter for promises getting resolved
(success) or rejected (failure)
O A promise can be –
O chained so you do not get code with callbacks
inside callbacks that flows out of the screen.
O errors propagate and can be handled at the
end.
O Assurance that previous call finishes before
next call.
Testing apps built with
Angular
Why Test ?
O Things do what they are expected to do.
O Future addition of code does not affect
previous features.
O Reduces effort in the long run but maybe
pain to write initially.
Unit tests in Angular
O Test stuffs like controllers, filters, services
and directives.
O Separation of DOM code makes testing
easier and possible.
O Using Jasmine like syntax and Jasmine
Spy instead of real browser.
Karma
O Karma is a test runner in JavaScript
O Test on real devices
O Control the whole workflow from
command line.
O Lightening fast !!
Scenario tests
O End to end tests involving complete
feature.
O E.g. first page of a blog should display N
blog posts.
O Done using Karma in Angular
Tools for productivity
Yeoman m/
O Lightning-fast scaffolding
O Built-in preview server
O Integrated package management
O An awesome build process
O Unit testing using PhantomJS
Batarang
O Chrome extension for angular
O Provides performance metrics, see
scopes and values of properties in them.
O Can change the values and see the
change in realtime.
IDE plugins
O Webstorm plugin is awesome.
O Sublime text angular plugin.
Angular Future
O Animations
O Breaking down into components
O alternative syntax work without the
$scope thing but not really useful.
O Much More…
Best practices
Because you can still write yucky code in Angular
No DOM manipulation in
controller
Things which are to be used
in multiple controllers ?
Use services
To wrap third party plugins
like JQuery date-picker, use
directives
Write tests.
This one is for me 
Use promises, they are
awesome.
Code organization
O Angular seed style
O Yeoman Style
O Any style you prefer
Learn more about it
O Angular Channel on Youtube
O http://www.yearofmoo.com
O egghead.io
O Stack Overflow
O Quora 

More Related Content

What's hot (20)

PDF
Angular js
Knoldus Inc.
 
PPTX
Angular 5 presentation for beginners
Imran Qasim
 
PPTX
Angular 9
Raja Vishnu
 
PDF
Angular material tutorial
HarikaReddy115
 
PPTX
Angular 9 New features
Ahmed Bouchefra
 
PDF
Dependency Injection pattern in Angular
Alexe Bogdan
 
PDF
Angular Routing Tutorial | AngularJS vs Angular Router | Angular Training | E...
Edureka!
 
PDF
Angular components
Sultan Ahmed
 
PPTX
Angular modules in depth
Christoffer Noring
 
PPTX
The Basics Angular JS
OrisysIndia
 
PPTX
Dive into Angular, part 5: Experience
Oleksii Prohonnyi
 
PPTX
Angular interview questions
Goa App
 
PPTX
Dive into Angular, part 1: Introduction
Oleksii Prohonnyi
 
PPTX
AngularJS Fundamentals + WebAPI
Eric Wise
 
PPT
introduction to Angularjs basics
Ravindra K
 
PPTX
Angular Workshop_Sarajevo2
Christoffer Noring
 
PDF
Adding User Management to Node.js
Dev_Events
 
PPTX
Training On Angular Js
Mahima Radhakrishnan
 
PDF
A gently introduction to AngularJS
Gregor Woiwode
 
PPT
Angular Seminar-js
Mindfire Solutions
 
Angular js
Knoldus Inc.
 
Angular 5 presentation for beginners
Imran Qasim
 
Angular 9
Raja Vishnu
 
Angular material tutorial
HarikaReddy115
 
Angular 9 New features
Ahmed Bouchefra
 
Dependency Injection pattern in Angular
Alexe Bogdan
 
Angular Routing Tutorial | AngularJS vs Angular Router | Angular Training | E...
Edureka!
 
Angular components
Sultan Ahmed
 
Angular modules in depth
Christoffer Noring
 
The Basics Angular JS
OrisysIndia
 
Dive into Angular, part 5: Experience
Oleksii Prohonnyi
 
Angular interview questions
Goa App
 
Dive into Angular, part 1: Introduction
Oleksii Prohonnyi
 
AngularJS Fundamentals + WebAPI
Eric Wise
 
introduction to Angularjs basics
Ravindra K
 
Angular Workshop_Sarajevo2
Christoffer Noring
 
Adding User Management to Node.js
Dev_Events
 
Training On Angular Js
Mahima Radhakrishnan
 
A gently introduction to AngularJS
Gregor Woiwode
 
Angular Seminar-js
Mindfire Solutions
 

Viewers also liked (8)

PDF
AngularJS introduction
Tania Gonzales
 
PDF
An introduction to AngularJS
Yogesh singh
 
PDF
AngularJS Introduction
Carlos Morales
 
PPTX
Hands On Intro to Node.js
Chris Cowan
 
PPTX
JS Frameworks - Angular Vs Backbone
Gourav Jain, MCTS®
 
PDF
How AngularJS Embraced Traditional Design Patterns
Ran Mizrahi
 
PDF
Introduction to AngularJS
Jamal Sinclair O'Garro
 
PDF
Getting Started with Angular 2
FITC
 
AngularJS introduction
Tania Gonzales
 
An introduction to AngularJS
Yogesh singh
 
AngularJS Introduction
Carlos Morales
 
Hands On Intro to Node.js
Chris Cowan
 
JS Frameworks - Angular Vs Backbone
Gourav Jain, MCTS®
 
How AngularJS Embraced Traditional Design Patterns
Ran Mizrahi
 
Introduction to AngularJS
Jamal Sinclair O'Garro
 
Getting Started with Angular 2
FITC
 
Ad

Similar to AngularJS Introduction (Talk given on Aug 5 2013) (20)

PDF
AngularJS in practice
Eugene Fidelin
 
PPTX
Angular js for Beginnners
Santosh Kumar Kar
 
PDF
Building Better Web Apps with Angular.js (SXSW 2014)
kbekessy
 
PPTX
AngularJS One Day Workshop
Shyam Seshadri
 
PPTX
ME vs WEB - AngularJS Fundamentals
Aviran Cohen
 
PDF
AngularJS Workshop
Gianluca Cacace
 
PDF
AngularJS in Production (CTO Forum)
Alex Ross
 
PPTX
Angular js 1.0-fundamentals
Venkatesh Narayanan
 
PDF
AngularJS Beginner Day One
Troy Miles
 
PPTX
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
murtazahaveliwala
 
PPTX
Intro to AngularJs
SolTech, Inc.
 
PPTX
Learning AngularJS - Complete coverage of AngularJS features and concepts
Suresh Patidar
 
PPTX
Angular workshop - Full Development Guide
Nitin Giri
 
PDF
Quick start with AngularJS
Iuliia Baranova
 
PPTX
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
murtazahaveliwala
 
PPTX
Angular js
Mauro Servienti
 
PPTX
AngularJS - a radically different way of building Single Page Apps
jivkopetiov
 
PPT
Angular js
yogi_solanki
 
PPTX
AngularJS.part1
Andrey Kolodnitsky
 
AngularJS in practice
Eugene Fidelin
 
Angular js for Beginnners
Santosh Kumar Kar
 
Building Better Web Apps with Angular.js (SXSW 2014)
kbekessy
 
AngularJS One Day Workshop
Shyam Seshadri
 
ME vs WEB - AngularJS Fundamentals
Aviran Cohen
 
AngularJS Workshop
Gianluca Cacace
 
AngularJS in Production (CTO Forum)
Alex Ross
 
Angular js 1.0-fundamentals
Venkatesh Narayanan
 
AngularJS Beginner Day One
Troy Miles
 
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
murtazahaveliwala
 
Intro to AngularJs
SolTech, Inc.
 
Learning AngularJS - Complete coverage of AngularJS features and concepts
Suresh Patidar
 
Angular workshop - Full Development Guide
Nitin Giri
 
Quick start with AngularJS
Iuliia Baranova
 
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
murtazahaveliwala
 
Angular js
Mauro Servienti
 
AngularJS - a radically different way of building Single Page Apps
jivkopetiov
 
Angular js
yogi_solanki
 
AngularJS.part1
Andrey Kolodnitsky
 
Ad

Recently uploaded (20)

PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Python basic programing language for automation
DanialHabibi2
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
July Patch Tuesday
Ivanti
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Python basic programing language for automation
DanialHabibi2
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 

AngularJS Introduction (Talk given on Aug 5 2013)

  • 1. AngularJS A better way to create web apps
  • 2. Agenda O Introduction to AngularJS O Some concepts used in AngularJS O Testing application built with Angular O Productivity and debugging tools O Code organization for large projects O Code demo as we go on
  • 3. Why AngularJS O HTML was created to transmit markup data on web. E.g. <b> tags for making text bold etc. O JavaScript added for interactivity. O And then comes AJAX and rich web apps. O And things get messed up by adding listeners after getting ids, callbacks and series of callbacks.
  • 4. What can AngularJS do O Less boilerplate code, faster development with less code and thus less tests and bugs. O Clean separation of DOM manipulation, business logic and views make things easier to test. O Declarative UI design means designers can focus on their work without even knowing JavaScript. O Support for cool stuff like promises.
  • 5. Angular components This is the real stuff
  • 6. Angular App Modules Models Controllers Templates (and Views) Directives Services Filters Routes
  • 7. Module O Angular apps have nothing like a main method O Directive ng-app used to bootstrap the module O E.g. <html ng-app=“someModule”> O A module can contain several other components O var module = angular.module(„someModule‟, []) O Second argument is an array of other modules which are requisites for the current module.
  • 8. Models O In Angular, model is any data that is reachable as property of $scope object O Can also create models inside templates using the ng-model directive e.g. <input type=“email” ng-model=“user.email”> O Can watch models for changes using $watch(watchFn, watchAction, deepWatch) O deepWatch is a boolean which allows to watch attributes of the object returned by watchFn
  • 9. Controllers O Set up the $scope O Add behavior to $scope O All business logic to be written in here. O Can use the one controller per template to create really traditional websites.
  • 10. Templates O Specify how things should show up and what happens on particular user actions. O Angular templates are not the view in MVC. Compiled angular templates are the views. O Declaratively create the UI using directives.
  • 11. Directives O One of the most important part of Angular O Allows you to extend HTML to define custom tags, attributes and classes for UI components. Jquery UI Angular Directives <div id=“tabs”> <ul> <li><a href=“#tabs-1”> Tab1</a></li> <li><a href=“#tabs-2”> Tab2</a></li> </ul> <div id=“#tabs-1>Content</div> <div id=“#tabs-2>Content2</div> </div> <script> $(function() { $(„#tabs”).tabs(); }); </script> <tab-set> <tab title=“Tab1”>Content</tab> <tab title=“Tab2”>Content2</tab> </tab-set> Now what if you need tabs on 10 pages in your whole application ?
  • 12. Services O Stuff required at various places e.g. by various controllers. O E.g. a http request to get list of blog posts in a blogging application would be required while showing all blog posts, in the admin edit/publish view etc. O Three different ways to create services i.e. services, factory and provider.
  • 13. Filters O Filters are transformations applies to objects O Not important to logic used to process models e.g. currency sign (like $) O E.g. lowerCase, sort etc. O Built-in filters like json O Custom filters can be defined with app.filter(„filterName‟, function (obj) {})
  • 14. Routes O Used to map templates and associated controller to URLs. O Supports HTML5 mode where you get history support. O That‟s it !
  • 15. Form Validation O Provides special support for two properties $valid, $invalid. O Can use them like <button ng- disabled=“!formName.$valid”> to disable a form until its all elements are correct. O polyfills for things like „required‟ on non- HTML browsers
  • 16. Server-side communication O $http wraps the XHR to provide asynchronous requests. O $resource for interacting with RESTful resources. (provided as a separate module in angular-resource.js) O $http requests support promises (talk about this later)
  • 17. Secrets of all the magic in Angular
  • 18. What actually happens O Template loads with all the angular directives O Angular script loads and after template loading finishes, it looks for ng-app to find application boundaries. O Compile phase – Angular walks the DOM to identify registered directive and transforms them. O Link Phase – This runs a link function for each directive which typically creates listeners on DOM or model and keeps view and model in sync .
  • 19. $scope O $scope allows binding and communication between controller and templates. O All the model objects are stored on it. O Makes sense not to store everything on it as it would degrade performance.
  • 20. Data binding O Binding two sources together and keep then updated of changes. O Angular supports two-way data binding. O Change in models get reflected everywhere in view. O Changes in view gets reflected in the model.
  • 21. Dependency Injection O Follows Law of Demeter or principle of least knowledge. O Instead of creating things, just ask for what is required. O E.g. to create a Car you need an engine so instead of calling an engine Factory in the constructor, pass an engine as parameter. O Makes things easier to test (e.g. mock $http), less code and easier to change later.
  • 22. Promises O A promise is an interface that deals with objects that are returned or get filled in at a future point in time. O Current approach with callbacks has problems of indentation while chaining multiple calls, losing errors reported between callbacks unless manually handled and doing real stuff in innermost callback.
  • 23. Promises O In angular a promise gives with a then function which takes two functions as a parameter for promises getting resolved (success) or rejected (failure) O A promise can be – O chained so you do not get code with callbacks inside callbacks that flows out of the screen. O errors propagate and can be handled at the end. O Assurance that previous call finishes before next call.
  • 24. Testing apps built with Angular
  • 25. Why Test ? O Things do what they are expected to do. O Future addition of code does not affect previous features. O Reduces effort in the long run but maybe pain to write initially.
  • 26. Unit tests in Angular O Test stuffs like controllers, filters, services and directives. O Separation of DOM code makes testing easier and possible. O Using Jasmine like syntax and Jasmine Spy instead of real browser.
  • 27. Karma O Karma is a test runner in JavaScript O Test on real devices O Control the whole workflow from command line. O Lightening fast !!
  • 28. Scenario tests O End to end tests involving complete feature. O E.g. first page of a blog should display N blog posts. O Done using Karma in Angular
  • 30. Yeoman m/ O Lightning-fast scaffolding O Built-in preview server O Integrated package management O An awesome build process O Unit testing using PhantomJS
  • 31. Batarang O Chrome extension for angular O Provides performance metrics, see scopes and values of properties in them. O Can change the values and see the change in realtime.
  • 32. IDE plugins O Webstorm plugin is awesome. O Sublime text angular plugin.
  • 33. Angular Future O Animations O Breaking down into components O alternative syntax work without the $scope thing but not really useful. O Much More…
  • 34. Best practices Because you can still write yucky code in Angular
  • 35. No DOM manipulation in controller
  • 36. Things which are to be used in multiple controllers ? Use services
  • 37. To wrap third party plugins like JQuery date-picker, use directives
  • 38. Write tests. This one is for me 
  • 39. Use promises, they are awesome.
  • 40. Code organization O Angular seed style O Yeoman Style O Any style you prefer
  • 41. Learn more about it O Angular Channel on Youtube O http://www.yearofmoo.com O egghead.io O Stack Overflow O Quora 

Editor's Notes

  • #14:  Angular comes with several filters, like currency, which we’ve seen:{{12.9 | currency}}This bit of code will display the following:$12.90We put this declaration in the view (rather than in the controller or model) because thedollar sign in front of the number is only important to humans, and not to the logic weuse to process the number.varhomeModule = angular.module(&apos;HomeModule&apos;, []);homeModule.filter(&apos;titleCase&apos;, function() {vartitleCaseFilter = function(input) {varwords = input.split(&apos; &apos;);for (vari = 0; i &lt; words.length; i++) {words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);}return words.join(&apos; &apos;);};return titleCaseFilter;});
  • #19: Script loadsAngular loads and looks for the ng-app directive to find the application boundaries.Compile phaseIn this phase, Angular walks the DOM to identify all the registered directives in thetemplate. For each directive, it then transforms the DOM based on the directive’srules (template, replace, transclude, and so on), and calls the compile functionif it exists. The result is a compiled template function, which will invoke the linkfunctions collected from all of the directives.Link phaseTo make the view dynamic, Angular then runs a link function for each directive.The link functions typically creates listeners on the DOM or the model. Theselisteners keep the view and the model in sync at all times. …. functions deal with transforming the template itself, and link functions deal with makinga dynamic connection between model and view. It is in this second phase that scopesare attached to the compiled link functions, and the directive becomes live throughdata binding. These two phases are separate for performance reasons. Compile functions execute onlyonce in the compile phase, whereas link functions are executed many times, once foreach instance of the directive. For example, let’s say you use ng-repeat over your directive.You don’t want to call compile, which causes a DOM-walk on each ngrepeatiteration. Instead, you want to compile once, then link.