SlideShare a Scribd company logo
Superheroic JavaScript MVW Framework
Table of contents 
•Short about AngularJS 
•AngularJS Best practices 
•AngularJS 1.3 
•AngularJS 2.0
• Modularity 
• Dependency injection 
• Two – way binding 
• Directives 
• Filters 
• Templating 
• Services 
• Testing ready 
…. Etc. 
Why angular is 
powerful
Best Practices
Directory structure
AngularJS Best Practices
Templates 
When angular still loading 
Use 
• Use ng-bind only for index.html or ng-cloak 
• For rest use {{ }} 
• When you need to set the src of an image dynamically 
use ng-src instead of src with {{ }} template. 
• When you need to set the href of an anchor tag 
dynamically use ng-href instead of href with {{ }} template. 
• Instead of using scope variable as string and using it with style 
attribute with {{ }}, use the directive ng-style with object-like 
parameters and scope variables as values: 
• Use $templateCache to decrease XHR calls (Optimization tips)
Now we have powerful templates
Directive 
• Name your directives with lowerCamelCase. 
• Use the dash-delimited format e.g ng-bind 
• Use prefixed names e.g my-customer 
• Reserved prefixes ng & ui 
• Clean up scope.$on('$destroy', ...) 
• use controller when you want to expose an API to other 
directives. Otherwise use link.
Business logic 
• Controllers 
oShould not do DOM manipulations 
oShould have view behavior 
• Services 
o Should not contain large DOM manipulations 
o Have logic independent of view 
o For session-level cache you can 
use $cacheFactory. (Optimization tips)
• Scope is read-only in view 
- Scope must refer to the model not to be a 
model (model is javascript object) 
• Scope is write-only in controller 
• $rootScope Unsubscribing watchers manually 
Demo with model 
Demo without model 
$scope
General 
• Use promises ($q) instead of callbacks. 
• Use $resource or RestAngular instead of $http when 
working in REST API 
• Use angular’s provided services instead of native , 
$timeout,$interval,$window, $document 
• AngularJS reserved prefix $, don’t use it when defining a 
variables 
• In DI order should be angular’s at first, then customs 
• Prefer the usage of controllers instead of ng-init
Cool, What about performance ?
Optimizing angular 
• Guaranteed watchers count is 2.000, link to count 
watchers 
• Limit DOM filters (Angular 1.2.x) 
• Each filter runs twice per $digest cycle 
• Use $filter provider - $filter('filter')(array, expression, comparator); 
• For content what should be rendered only once use 
bindonce (Angular 1.2.x) or {{::myVar}} (Angular 1.3.x) 
• $timeout & $interval functions has third argument which 
will skip $digest loop
Optimizing angular 
• Don’t write crazy things in $watch callback it will kill your 
app 
• $watch has two comparison modes 
• referential (default) - quick, shallow comparison 
• deep - slow, recurses through objects for a deep comparison; also 
makes a deep copy of the watched value each time it changes 
• $watchCollection function is a sort-of mid-ground between 
the two $watch() configurations 
• referential (default) - quick, shallow comparison lika default $watch 
• goes one-level deep and performs an additional, shallow reference 
check of the top level items in the collection 
• Avoid deep $watch whenever possible, instead use 
$watchCollection
Optimizing angular 
• ng-repeat - track by $index 
• By default, ng-repeat creates a dom node for each item and 
destroys that dom node when the item is removed. 
• With track by $index, it will reuse dom nodes. 
• Very Bad Practice: $$postDigest 
• $$ means private to Angular, so be aware that the interface is not 
stable 
• Fires a callback after the current $digest cycle completes 
• $timeout or $evalAsync ?
Angular 1.3
Angular 1.3 
• Performance (3-4x faster) 
• DOM manipulation 
• Dirty checking 
• Memory usage 
• Animations 
• Forms 
• Flexible validation 
• Debouncing 
• Error reporting 
• Other 
• $watchGroup 
• templateNamespace
Performance
Performance
Performance 
• $httpProvider.useApplyAsync(true); 
• one-time binding
form - ngModel 
• ngModel.$validators 
• $parsers + $formatters 
• ngModel.$asyncValidators 
• ngModel.ngModelOptions
form - ngModel.$validators
form - ngModel.$validators
form - ngModel.$asyncValidators
form - ngModel.ngModelOptions.debounce
form - ngModel.ngModelOptions-getter/settter
form - ngModel.ngModelOptions.updateOn
form - ngMessages
form - ngMessages
form - ngMessages
form - ngMessages
Other - composable SVG
Other - allOrNothing
Other - $watchGroup
Other - strictDI 
ng-europe 2014 
video | follow-up post
Angular 2.0
Angular 2.0 
controllers 
DDO 
$scope 
angular.module 
jqLite 
generic binding syntax 
DI query mechanism 
benchpress 
WTF instrumentation 
ng-europe 2014 
video | follow-up post
References 
AngularJS blog post 
AngularJS style guide 
ng-europe 
Ben Nadel’s blog 
Todd Motto’s blog 
ng-newsletter 
angular-test-patterns
Q&A
Narek Mamikonyan 
@nmamikonyan 
narekmamikonyan@gmail.com

More Related Content

What's hot (20)

PDF
Advanced Tips & Tricks for using Angular JS
Simon Guest
 
PPTX
Why angular js Framework
Sakthi Bro
 
PDF
AngularJS 101 - Everything you need to know to get started
Stéphane Bégaudeau
 
PPTX
Angular js
Behind D Walls
 
PPTX
Angular js
Dinusha Nandika
 
PPTX
Step by Step - AngularJS
Infragistics
 
PPTX
The AngularJS way
Boyan Mihaylov
 
PPTX
Angular js PPT
Imtiyaz Ahmad Khan
 
PPTX
AngularJS intro
dizabl
 
PPTX
AngularJS Beginners Workshop
Sathish VJ
 
PDF
Angularjs architecture
Michael He
 
PPTX
Understanding angular js
Aayush Shrestha
 
PPTX
Angular js 1.3 basic tutorial
Al-Mutaz Bellah Salahat
 
PDF
AngularJS application architecture
Gabriele Falace
 
PPTX
AngularJS One Day Workshop
Shyam Seshadri
 
PPTX
Front end development with Angular JS
Bipin
 
PDF
Gettings started with the superheroic JavaScript library AngularJS
Armin Vieweg
 
PDF
AngularJS best-practices
Henry Tao
 
PPTX
Introduction to AngularJS
David Parsons
 
PDF
AngularJS Basic Training
Cornel Stefanache
 
Advanced Tips & Tricks for using Angular JS
Simon Guest
 
Why angular js Framework
Sakthi Bro
 
AngularJS 101 - Everything you need to know to get started
Stéphane Bégaudeau
 
Angular js
Behind D Walls
 
Angular js
Dinusha Nandika
 
Step by Step - AngularJS
Infragistics
 
The AngularJS way
Boyan Mihaylov
 
Angular js PPT
Imtiyaz Ahmad Khan
 
AngularJS intro
dizabl
 
AngularJS Beginners Workshop
Sathish VJ
 
Angularjs architecture
Michael He
 
Understanding angular js
Aayush Shrestha
 
Angular js 1.3 basic tutorial
Al-Mutaz Bellah Salahat
 
AngularJS application architecture
Gabriele Falace
 
AngularJS One Day Workshop
Shyam Seshadri
 
Front end development with Angular JS
Bipin
 
Gettings started with the superheroic JavaScript library AngularJS
Armin Vieweg
 
AngularJS best-practices
Henry Tao
 
Introduction to AngularJS
David Parsons
 
AngularJS Basic Training
Cornel Stefanache
 

Viewers also liked (11)

PPTX
Angularjs Performance
Steven Lambert
 
PPTX
Data structure using c module 1
smruti sarangi
 
PPT
Css best practices style guide and tips
Chris Love
 
PPTX
AngularJS best practices
Filip Bruun Bech-Larsen
 
PDF
The Art of AngularJS in 2015
Matt Raible
 
PDF
CSS3 Media Queries
Russ Weakley
 
PPTX
Media queries and frameworks
Nicole Ryan
 
PDF
Beyond Media Queries: Anatomy of an Adaptive Web Design
Brad Frost
 
PPTX
Mobile Email Design, Strategies, Workflow and Best Practices
Litmus
 
PDF
CSS Best practice
Russ Weakley
 
PDF
New Features in Angular 1.5
Kenichi Kanai
 
Angularjs Performance
Steven Lambert
 
Data structure using c module 1
smruti sarangi
 
Css best practices style guide and tips
Chris Love
 
AngularJS best practices
Filip Bruun Bech-Larsen
 
The Art of AngularJS in 2015
Matt Raible
 
CSS3 Media Queries
Russ Weakley
 
Media queries and frameworks
Nicole Ryan
 
Beyond Media Queries: Anatomy of an Adaptive Web Design
Brad Frost
 
Mobile Email Design, Strategies, Workflow and Best Practices
Litmus
 
CSS Best practice
Russ Weakley
 
New Features in Angular 1.5
Kenichi Kanai
 
Ad

Similar to AngularJS Best Practices (20)

PPTX
Optimizing a large angular application (ng conf)
A K M Zahiduzzaman
 
PPTX
Bhuvi ppt zerobug
BhuviS3
 
PPT
Angular js meetup
Pierre-Yves Gicquel
 
PPT
angularjsmeetup-150303044616-conversion-gate01
Teo E
 
PPTX
Dive into Angular, part 3: Performance
Oleksii Prohonnyi
 
PDF
AngularJS performance & production tips
Nir Kaufman
 
PDF
AngularJS - Overcoming performance issues. Limits.
Dragos Mihai Rusu
 
PDF
Dragos Rusu - Angular JS - Overcoming Performance Issues - CodeCamp-10-may-2014
Codecamp Romania
 
PDF
Javascript Memory leaks and Performance & Angular
Erik Guzman
 
PPTX
AngularJS.part1
Andrey Kolodnitsky
 
PPTX
Optimizing Angular Performance in Enterprise Single Page Apps
Morgan Stone
 
ODP
AngularJs Crash Course
Keith Bloomfield
 
PPTX
5 angularjs features
Alexey (Mr_Mig) Migutsky
 
PDF
AngularJS in practice
Eugene Fidelin
 
PPTX
Angular js 1.0-fundamentals
Venkatesh Narayanan
 
PDF
Angular Performance: Then, Now and the Future. Todd Motto
Future Insights
 
PDF
AngularJS Basics
Ravi Mone
 
ODP
Angular js-crash-course
Keith Bloomfield
 
PPTX
AngularJS Introduction (Talk given on Aug 5 2013)
Abhishek Anand
 
PPTX
Introduction to AngularJs
murtazahaveliwala
 
Optimizing a large angular application (ng conf)
A K M Zahiduzzaman
 
Bhuvi ppt zerobug
BhuviS3
 
Angular js meetup
Pierre-Yves Gicquel
 
angularjsmeetup-150303044616-conversion-gate01
Teo E
 
Dive into Angular, part 3: Performance
Oleksii Prohonnyi
 
AngularJS performance & production tips
Nir Kaufman
 
AngularJS - Overcoming performance issues. Limits.
Dragos Mihai Rusu
 
Dragos Rusu - Angular JS - Overcoming Performance Issues - CodeCamp-10-may-2014
Codecamp Romania
 
Javascript Memory leaks and Performance & Angular
Erik Guzman
 
AngularJS.part1
Andrey Kolodnitsky
 
Optimizing Angular Performance in Enterprise Single Page Apps
Morgan Stone
 
AngularJs Crash Course
Keith Bloomfield
 
5 angularjs features
Alexey (Mr_Mig) Migutsky
 
AngularJS in practice
Eugene Fidelin
 
Angular js 1.0-fundamentals
Venkatesh Narayanan
 
Angular Performance: Then, Now and the Future. Todd Motto
Future Insights
 
AngularJS Basics
Ravi Mone
 
Angular js-crash-course
Keith Bloomfield
 
AngularJS Introduction (Talk given on Aug 5 2013)
Abhishek Anand
 
Introduction to AngularJs
murtazahaveliwala
 
Ad

Recently uploaded (20)

PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Digital Circuits, important subject in CS
contactparinay1
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 

AngularJS Best Practices

  • 2. Table of contents •Short about AngularJS •AngularJS Best practices •AngularJS 1.3 •AngularJS 2.0
  • 3. • Modularity • Dependency injection • Two – way binding • Directives • Filters • Templating • Services • Testing ready …. Etc. Why angular is powerful
  • 7. Templates When angular still loading Use • Use ng-bind only for index.html or ng-cloak • For rest use {{ }} • When you need to set the src of an image dynamically use ng-src instead of src with {{ }} template. • When you need to set the href of an anchor tag dynamically use ng-href instead of href with {{ }} template. • Instead of using scope variable as string and using it with style attribute with {{ }}, use the directive ng-style with object-like parameters and scope variables as values: • Use $templateCache to decrease XHR calls (Optimization tips)
  • 8. Now we have powerful templates
  • 9. Directive • Name your directives with lowerCamelCase. • Use the dash-delimited format e.g ng-bind • Use prefixed names e.g my-customer • Reserved prefixes ng & ui • Clean up scope.$on('$destroy', ...) • use controller when you want to expose an API to other directives. Otherwise use link.
  • 10. Business logic • Controllers oShould not do DOM manipulations oShould have view behavior • Services o Should not contain large DOM manipulations o Have logic independent of view o For session-level cache you can use $cacheFactory. (Optimization tips)
  • 11. • Scope is read-only in view - Scope must refer to the model not to be a model (model is javascript object) • Scope is write-only in controller • $rootScope Unsubscribing watchers manually Demo with model Demo without model $scope
  • 12. General • Use promises ($q) instead of callbacks. • Use $resource or RestAngular instead of $http when working in REST API • Use angular’s provided services instead of native , $timeout,$interval,$window, $document • AngularJS reserved prefix $, don’t use it when defining a variables • In DI order should be angular’s at first, then customs • Prefer the usage of controllers instead of ng-init
  • 13. Cool, What about performance ?
  • 14. Optimizing angular • Guaranteed watchers count is 2.000, link to count watchers • Limit DOM filters (Angular 1.2.x) • Each filter runs twice per $digest cycle • Use $filter provider - $filter('filter')(array, expression, comparator); • For content what should be rendered only once use bindonce (Angular 1.2.x) or {{::myVar}} (Angular 1.3.x) • $timeout & $interval functions has third argument which will skip $digest loop
  • 15. Optimizing angular • Don’t write crazy things in $watch callback it will kill your app • $watch has two comparison modes • referential (default) - quick, shallow comparison • deep - slow, recurses through objects for a deep comparison; also makes a deep copy of the watched value each time it changes • $watchCollection function is a sort-of mid-ground between the two $watch() configurations • referential (default) - quick, shallow comparison lika default $watch • goes one-level deep and performs an additional, shallow reference check of the top level items in the collection • Avoid deep $watch whenever possible, instead use $watchCollection
  • 16. Optimizing angular • ng-repeat - track by $index • By default, ng-repeat creates a dom node for each item and destroys that dom node when the item is removed. • With track by $index, it will reuse dom nodes. • Very Bad Practice: $$postDigest • $$ means private to Angular, so be aware that the interface is not stable • Fires a callback after the current $digest cycle completes • $timeout or $evalAsync ?
  • 18. Angular 1.3 • Performance (3-4x faster) • DOM manipulation • Dirty checking • Memory usage • Animations • Forms • Flexible validation • Debouncing • Error reporting • Other • $watchGroup • templateNamespace
  • 22. form - ngModel • ngModel.$validators • $parsers + $formatters • ngModel.$asyncValidators • ngModel.ngModelOptions
  • 36. Other - strictDI ng-europe 2014 video | follow-up post
  • 38. Angular 2.0 controllers DDO $scope angular.module jqLite generic binding syntax DI query mechanism benchpress WTF instrumentation ng-europe 2014 video | follow-up post
  • 39. References AngularJS blog post AngularJS style guide ng-europe Ben Nadel’s blog Todd Motto’s blog ng-newsletter angular-test-patterns
  • 40. Q&A

Editor's Notes

  • #39: Image source: http://pixabay.com/en/tombstone-rip-dead-death-funeral-159792/