SlideShare a Scribd company logo
1 of 47
Download to read offline
$ 
ANGULARJS 
ANGULAR BASICS 
2014
FACTS 
100% Javascript and 100% Client Side 
It is a framework not a library 
Won't make your application look better 
Alternatives: EmberJS and Backbone 
MVC Pattern
UNDER THE HOOD 
Routing 
Caching 
History 
*Dependency Injection 
Data Binding 
Templating 
Timing 
Compiling
CONCEPTS
MVC PATTERN IN ANGULARJS
TERMINOLOGY 
Model - application data 
$scope - the bridge between application data, view and 
controller 
View - what the user sees 
Template - blueprints for the view 
Controller - application behavior 
Directive - reusable component 
Module - how the application is bootstrapped
SAMPLE 
TEMPLATE 
<div ng-controller='nsp.Controller'> 
<span>{{value}}</span> 
</div> 
CONTROLLER 
(function(ns) { 
ns.CompanyController = function($scope) { 
$scope.value = 'Text Value'; 
} })(nsp) 
OUTPUT 
<div ng-controller='nsp.Controller'> 
<span>Text Value</span> 
</div>
SCOPE 
IN THE $SCOPE YOU STORE EVERYTHING THAT YOU NEED TO 
DISPLAY ON THE VIEW OR YOU NEED TO CALL FROM THE VIEW
SCOPE VIEW 
THE SCOPE HAS A HIERARCHICAL STRUCTURE TO MIMIC THE DOM AND CAN INHERIT FROM 
PARENT SCOPES. ALSO CAN PASS EVENTS TO PARENT OR CHILD SCOPES.
BASIC APPLICATION
CODE ORGANIZATION 
root-app-folder 
├── index.html 
├── scripts 
│ ├── controllers 
│ │ └── ... 
│ ├── directives 
│ │ └── ... 
│ ├── filters 
│ │ └── ... 
│ ├── services 
│ │ └── ... 
│ ├── vendor 
│ │ ├── angular.min.js 
│ └── app.js 
├── styles 
└── views || partials 
├── main.html
INDEX.HTM(L) FILE 
<html ng-app="demo"> 
<head> 
<link href="styles/basic.css" rel="stylesheet" /> 
</head> 
<body> 
<div ng-view> 
</div> 
</body> 
<!--Load scripts down here--> 
</html>
DEFINE FIRST PAGE - CONTROLLER 
(function(ns) { 
ns.MainController = function($scope) { 
$scope.message = 'Hello World'; 
} 
})(org.sample)
DEFINE FIRST PAGE - VIEW (PARTIAL) 
<span> 
<p>Scope messsage value is {{message}}>/p> 
</span>
TIE EVERYTHING TOGETHER 
(function(ns) { 
var mainModule = angular.module('demo',['ngRoute']); 
mainModule.config([ 
'$routeProvider', function($routeProvider) { 
$routeProvider.when('/main', { 
templateUrl : 'partials/main.html', 
controller : ns.MainController 
}).otherwise({ 
redirectTo : '/main' 
}); 
} 
]); 
})(org.sample);
BINDINGS 
THE DECLARATIVE WAY
TWO WAY DATA BINDING 
<span>{{variableName}}</span> 
<input ng-model="variableName" />
INTERNALS
WATCHERS 
For each binding a $watcher is stored 
Each digest cycle is checking all the watchers 
If the value was changed then the watcher callback is executed 
The digest is executed multiple times *
MANUAL INVOKATION 
SOMETIMES WE NEED TO APPLY THE SCOPE MANUALLY 
WHY?
TOOLS
CONTROLLERS 
PROVIDES A WORKBENCH WHERE YOU CAN DEFINE 
PROPERTIES OR FUNCTIONS ACESSIBLE FROM THE VIEW 
HAS ACESS TO $SCOPE, AND INJECTABLES
DIRECTIVES 
DEFINES THE BEHAVIOR FOR REUSABLE COMPONENTS 
A DIRECTIVE IS NEEDED ONCE YOU NEED TO ACESS THE DOM 
ELEMENT
SERVICES 
SINGLETON OBJECTS THAT CARRY OUT SPECIFIC TASKS 
SERVICE OBJECTS ARE INJECTABLE IN THE CONSTRUCTOR 
PERSISTED STATE BETWEEN PAGE NAVIGATION
FILTERS 
COMPONENT PROVIDERS USED TO FORMAT DATA 
USED IN VIEWS IN ORDER NOT TO INCLUDE LOGIC IN THE 
CONTROLLER 
EXAMPLE: DATE FORMATTER, CURRENCY ETC.
OUT OF THE BOX
NG-REPEAT
NG-HIDE, NG-SHOW, NG-IF
NG-MODEL
NG-CHANGE
NG-CLICK
DIRECTIVE 
http://amitgharat.wordpress.com/2013/06/08/the-hitchhikers-guide- 
to-the-directive/
myModule.directive('directiveName', function (injectables) { 
return { 
restrict: 'A', 
template: '<div></div>', 
templateUrl: 'directive.html', 
replace: false, 
priority: 0, 
transclude: false, 
scope: false, 
terminal: false, 
require: false, 
controller: function($scope, $element, $attrs, $transclude, otherInjectables) { ... }, 
compile: function compile(tElement, tAttrs, transclude) { 
return { 
pre: function preLink(scope, iElement, iAttrs, controller) { ... }, 
post: function postLink(scope, iElement, iAttrs, controller) { ... } 
} 
}, 
link: function postLink(scope, iElement, iAttrs) { ... } 
}; 
});
RESTRICT 
This simply restricts the way you can define a directive. As we’ve 
seen before, we can restrict it as: 
E: elements 
A: attributes 
C: class names (CSS) 
M: comments
TEMPLATE 
This basically replaces the existing contents of an element
TEMPLATEURL 
Loads a template url and replaces the existing contents of an 
element
REPLACE 
Replaces the element where the directive was defined
TRANSCLUDE 
Prevents from replacing element contents and inserts the 
content where ng-transclude directive can be found
SCOPE 
false - Is the default option which does not create a new scope 
for a directive but shares the scope with its parent. 
true - Creates a new scope but prototypically inherits from the 
parent scope. 
'isolate' - Creates an isolated scope which does not 
prototypically inherit from the parent 
@ – binds the value of parent scope property (which always 
a string) to the local scope. So the value you want to pass in 
should be wrapped in {{}}. Remember `a` in braces. 
= – binds parent scope property directly which will be 
evaluated before being passed in. 
& – binds an expression or method which will be executed in 
the context of the scope it belongs.
CONTROLLER 
This can be treated as a control room for a directive. You can 
either bind properties/methods to $scope available or this 
keyword.
COMPILE 
This is the place where you can do the DOM manipulation.
LINK 
Its job is to bind a scope with a DOM resulted in a 2-way data 
binding. You have access to scope here unlike compile function 
so that you can create custom listeners using $watch method.
LINKS 
http://www.cheatography.com/proloser/cheat-sheets/ 
angularjs/
THE END
AngularJS Basic Training

More Related Content

What's hot

Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
Wei Ru
 

What's hot (20)

Angular js
Angular jsAngular js
Angular js
 
AngularJS Framework
AngularJS FrameworkAngularJS Framework
AngularJS Framework
 
AngularJS: an introduction
AngularJS: an introductionAngularJS: an introduction
AngularJS: an introduction
 
Angular js
Angular jsAngular js
Angular js
 
Angular JS - Introduction
Angular JS - IntroductionAngular JS - Introduction
Angular JS - Introduction
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
 
Angularjs architecture
Angularjs architectureAngularjs architecture
Angularjs architecture
 
Getting Started with Angular JS
Getting Started with Angular JSGetting Started with Angular JS
Getting Started with Angular JS
 
Why angular js Framework
Why angular js Framework Why angular js Framework
Why angular js Framework
 
AngularJS Best Practices
AngularJS Best PracticesAngularJS Best Practices
AngularJS Best Practices
 
AngularJs Crash Course
AngularJs Crash CourseAngularJs Crash Course
AngularJs Crash Course
 
AngularJS in 60ish Minutes
AngularJS in 60ish MinutesAngularJS in 60ish Minutes
AngularJS in 60ish Minutes
 
Angular js PPT
Angular js PPTAngular js PPT
Angular js PPT
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
AngularJs
AngularJsAngularJs
AngularJs
 
Angular Js Basics
Angular Js BasicsAngular Js Basics
Angular Js Basics
 
Modern Web Application Development Workflow - EclipseCon Europe 2014
Modern Web Application Development Workflow - EclipseCon Europe 2014Modern Web Application Development Workflow - EclipseCon Europe 2014
Modern Web Application Development Workflow - EclipseCon Europe 2014
 
5 angularjs features
5 angularjs features5 angularjs features
5 angularjs features
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get started
 

Similar to AngularJS Basic Training

Similar to AngularJS Basic Training (20)

AngularJs-training
AngularJs-trainingAngularJs-training
AngularJs-training
 
Basics of AngularJS
Basics of AngularJSBasics of AngularJS
Basics of AngularJS
 
Introduction to angular js
Introduction to angular jsIntroduction to angular js
Introduction to angular js
 
AngularJS.part1
AngularJS.part1AngularJS.part1
AngularJS.part1
 
AngularJS Workshop
AngularJS WorkshopAngularJS Workshop
AngularJS Workshop
 
Angular js
Angular jsAngular js
Angular js
 
Angular Js Get Started - Complete Course
Angular Js Get Started - Complete CourseAngular Js Get Started - Complete Course
Angular Js Get Started - Complete Course
 
Get AngularJS Started!
Get AngularJS Started!Get AngularJS Started!
Get AngularJS Started!
 
Angular js
Angular jsAngular js
Angular js
 
Patterns Are Good For Managers
Patterns Are Good For ManagersPatterns Are Good For Managers
Patterns Are Good For Managers
 
AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014
 
AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014
 
Angular Performance: Then, Now and the Future. Todd Motto
Angular Performance: Then, Now and the Future. Todd MottoAngular Performance: Then, Now and the Future. Todd Motto
Angular Performance: Then, Now and the Future. Todd Motto
 
Angular workshop - Full Development Guide
Angular workshop - Full Development GuideAngular workshop - Full Development Guide
Angular workshop - Full Development Guide
 
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
 
AEM Sightly Deep Dive
AEM Sightly Deep DiveAEM Sightly Deep Dive
AEM Sightly Deep Dive
 
angularJs Workshop
angularJs WorkshopangularJs Workshop
angularJs Workshop
 
Angular.js Primer in Aalto University
Angular.js Primer in Aalto UniversityAngular.js Primer in Aalto University
Angular.js Primer in Aalto University
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle StudiosAngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
 
Building Single Page Application (SPA) with Symfony2 and AngularJS
Building Single Page Application (SPA) with Symfony2 and AngularJSBuilding Single Page Application (SPA) with Symfony2 and AngularJS
Building Single Page Application (SPA) with Symfony2 and AngularJS
 

Recently uploaded

Recently uploaded (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

AngularJS Basic Training

  • 1. $ ANGULARJS ANGULAR BASICS 2014
  • 2.
  • 3. FACTS 100% Javascript and 100% Client Side It is a framework not a library Won't make your application look better Alternatives: EmberJS and Backbone MVC Pattern
  • 4. UNDER THE HOOD Routing Caching History *Dependency Injection Data Binding Templating Timing Compiling
  • 6. MVC PATTERN IN ANGULARJS
  • 7. TERMINOLOGY Model - application data $scope - the bridge between application data, view and controller View - what the user sees Template - blueprints for the view Controller - application behavior Directive - reusable component Module - how the application is bootstrapped
  • 8. SAMPLE TEMPLATE <div ng-controller='nsp.Controller'> <span>{{value}}</span> </div> CONTROLLER (function(ns) { ns.CompanyController = function($scope) { $scope.value = 'Text Value'; } })(nsp) OUTPUT <div ng-controller='nsp.Controller'> <span>Text Value</span> </div>
  • 9. SCOPE IN THE $SCOPE YOU STORE EVERYTHING THAT YOU NEED TO DISPLAY ON THE VIEW OR YOU NEED TO CALL FROM THE VIEW
  • 10. SCOPE VIEW THE SCOPE HAS A HIERARCHICAL STRUCTURE TO MIMIC THE DOM AND CAN INHERIT FROM PARENT SCOPES. ALSO CAN PASS EVENTS TO PARENT OR CHILD SCOPES.
  • 12. CODE ORGANIZATION root-app-folder ├── index.html ├── scripts │ ├── controllers │ │ └── ... │ ├── directives │ │ └── ... │ ├── filters │ │ └── ... │ ├── services │ │ └── ... │ ├── vendor │ │ ├── angular.min.js │ └── app.js ├── styles └── views || partials ├── main.html
  • 13. INDEX.HTM(L) FILE <html ng-app="demo"> <head> <link href="styles/basic.css" rel="stylesheet" /> </head> <body> <div ng-view> </div> </body> <!--Load scripts down here--> </html>
  • 14. DEFINE FIRST PAGE - CONTROLLER (function(ns) { ns.MainController = function($scope) { $scope.message = 'Hello World'; } })(org.sample)
  • 15. DEFINE FIRST PAGE - VIEW (PARTIAL) <span> <p>Scope messsage value is {{message}}>/p> </span>
  • 16. TIE EVERYTHING TOGETHER (function(ns) { var mainModule = angular.module('demo',['ngRoute']); mainModule.config([ '$routeProvider', function($routeProvider) { $routeProvider.when('/main', { templateUrl : 'partials/main.html', controller : ns.MainController }).otherwise({ redirectTo : '/main' }); } ]); })(org.sample);
  • 18.
  • 19. TWO WAY DATA BINDING <span>{{variableName}}</span> <input ng-model="variableName" />
  • 21. WATCHERS For each binding a $watcher is stored Each digest cycle is checking all the watchers If the value was changed then the watcher callback is executed The digest is executed multiple times *
  • 22. MANUAL INVOKATION SOMETIMES WE NEED TO APPLY THE SCOPE MANUALLY WHY?
  • 23. TOOLS
  • 24. CONTROLLERS PROVIDES A WORKBENCH WHERE YOU CAN DEFINE PROPERTIES OR FUNCTIONS ACESSIBLE FROM THE VIEW HAS ACESS TO $SCOPE, AND INJECTABLES
  • 25. DIRECTIVES DEFINES THE BEHAVIOR FOR REUSABLE COMPONENTS A DIRECTIVE IS NEEDED ONCE YOU NEED TO ACESS THE DOM ELEMENT
  • 26. SERVICES SINGLETON OBJECTS THAT CARRY OUT SPECIFIC TASKS SERVICE OBJECTS ARE INJECTABLE IN THE CONSTRUCTOR PERSISTED STATE BETWEEN PAGE NAVIGATION
  • 27. FILTERS COMPONENT PROVIDERS USED TO FORMAT DATA USED IN VIEWS IN ORDER NOT TO INCLUDE LOGIC IN THE CONTROLLER EXAMPLE: DATE FORMATTER, CURRENCY ETC.
  • 28. OUT OF THE BOX
  • 35. myModule.directive('directiveName', function (injectables) { return { restrict: 'A', template: '<div></div>', templateUrl: 'directive.html', replace: false, priority: 0, transclude: false, scope: false, terminal: false, require: false, controller: function($scope, $element, $attrs, $transclude, otherInjectables) { ... }, compile: function compile(tElement, tAttrs, transclude) { return { pre: function preLink(scope, iElement, iAttrs, controller) { ... }, post: function postLink(scope, iElement, iAttrs, controller) { ... } } }, link: function postLink(scope, iElement, iAttrs) { ... } }; });
  • 36. RESTRICT This simply restricts the way you can define a directive. As we’ve seen before, we can restrict it as: E: elements A: attributes C: class names (CSS) M: comments
  • 37. TEMPLATE This basically replaces the existing contents of an element
  • 38. TEMPLATEURL Loads a template url and replaces the existing contents of an element
  • 39. REPLACE Replaces the element where the directive was defined
  • 40. TRANSCLUDE Prevents from replacing element contents and inserts the content where ng-transclude directive can be found
  • 41. SCOPE false - Is the default option which does not create a new scope for a directive but shares the scope with its parent. true - Creates a new scope but prototypically inherits from the parent scope. 'isolate' - Creates an isolated scope which does not prototypically inherit from the parent @ – binds the value of parent scope property (which always a string) to the local scope. So the value you want to pass in should be wrapped in {{}}. Remember `a` in braces. = – binds parent scope property directly which will be evaluated before being passed in. & – binds an expression or method which will be executed in the context of the scope it belongs.
  • 42. CONTROLLER This can be treated as a control room for a directive. You can either bind properties/methods to $scope available or this keyword.
  • 43. COMPILE This is the place where you can do the DOM manipulation.
  • 44. LINK Its job is to bind a scope with a DOM resulted in a 2-way data binding. You have access to scope here unlike compile function so that you can create custom listeners using $watch method.