SlideShare a Scribd company logo
Angularjs
Anti-Patterns
The List
● Using $() instead of angular.element()
● Using jQuery to manipulate the DOM
● Looking at $scope.$$phase
● Not making ng-model an object
● Controllers doing too much or too bloated
o Manipulating DOM or model/data
o Throwing everything on $scope
o Overusing $rootscope
● Not doing things the “Angular” way
o Programing like you would in jQuery
o Manipulating the view instead of the model
o Not using Angular built-ins ($timeout, ng-show, ng-click, etc.)
● Not encapsulating 3rd party libraries in an Angular service
$() vs. angular.element
If jQuery is available, angular.element is an alias for the jQuery
function. If jQuery is not available, angular.element delegates to Angular's
built-in subset of jQuery, called "jQuery lite" or "jqLite."“
Thus, angular.element is $(), so just use angular.element.
Side note: don’t wrap in $element in a directive in angular.element, it already
is one.
From the docs:
jQuery DOM manipulation
jQuery DOM manipulation isn’t detected by Angular’s dirty checker.
Generating DOM with jQuery doesn’t create any Angular bindings.
From the docs:
Stop trying to use jQuery to modify the DOM… . Really. That includes
adding elements, removing elements, retrieving their contents, showing
and hiding them. Use built-in directives, or write your own where
necessary, to do your DOM manipulation
“
$scope.$$phase
For future-proofing reasons, you should not use $$phase
“
From Angular:
Typically $scope.$$phase is used to conditionally call $apply.
Instead, move the $apply to where you know you’re outside Angular.
If nothing else, use $timeout.
Code example
ng-model not an object
Code example
If ng-model is a primitive type (string, number, boolean, null, undefined) any
references in a child controller or directive won’t be doubly bound.
If it’s a complex type (object or array), it will be doubly bound.
Always make ng-model an object.
Controller Bloat
Don’t just throw everything on $scope or you’ll end up with $scope soup.
Think of $scope as the public interface from your view to your data. Only
expose what you need in the view.
This goes for $rootscope as well.
Think of $rootscope as the global namespace, and we all know not to pollute
the global namespace.
Controller Bloat
Controllers should be small and lean.
Things that don’t belong in a controller:
● DOM manipulation (belongs in a directive)
● Ajax or $http (belongs in a service)
● Model/data (belongs in a service)
● Model/data manipulation (belongs in the same service as the data)
● Business logic (belongs in a service)
Exercise: convert bloated controller to lean controller
The “Angular” Way
Exercise: convert jQuery to Angular
Programing in Angular is very different than programming in jQuery
When using Angular, try not to manipulate the view (DOM), instead
manipulate the model (data and how it is presented)
Don’t forget about all the Angular built-in goodies like ng-show, ng-click, etc.
3rd Party Libraries
When using 3rd party libraries, always wrap them in an Angular service.
angular.module(‘myApp’).factory(‘_’, function() {
return window._;
});
angular.module(‘myApp’).controller(‘myCtrl’, function(_) {
// can now use library as _
});
3rd Party Libraries
By wrapping the library, you now allow Angular’s dependency injection to
handle everything.
This also makes testing easier as you can now inject a mock of the library
into the unit test.

More Related Content

What's hot

Filters in AngularJS
Filters in AngularJSFilters in AngularJS
Filters in AngularJS
Brajesh Yadav
 

What's hot (20)

Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
Advanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JSAdvanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JS
 
Building an End-to-End AngularJS Application
Building an End-to-End AngularJS ApplicationBuilding an End-to-End AngularJS Application
Building an End-to-End AngularJS Application
 
Angular js - the beginning
Angular js - the beginningAngular js - the beginning
Angular js - the beginning
 
AngularJs Crash Course
AngularJs Crash CourseAngularJs Crash Course
AngularJs Crash Course
 
AngularJS - What is it & Why is it awesome ? (with demos)
AngularJS - What is it & Why is it awesome ? (with demos)AngularJS - What is it & Why is it awesome ? (with demos)
AngularJS - What is it & Why is it awesome ? (with demos)
 
Angular js for beginners
Angular js for beginnersAngular js for beginners
Angular js for beginners
 
AngularJS Framework
AngularJS FrameworkAngularJS Framework
AngularJS Framework
 
Angularjs Performance
Angularjs PerformanceAngularjs Performance
Angularjs Performance
 
AngularJs presentation
AngularJs presentation AngularJs presentation
AngularJs presentation
 
AngularJS Best Practices
AngularJS Best PracticesAngularJS Best Practices
AngularJS Best Practices
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 
AngularJS intro
AngularJS introAngularJS intro
AngularJS intro
 
AngularJs
AngularJsAngularJs
AngularJs
 
Filters in AngularJS
Filters in AngularJSFilters in AngularJS
Filters in AngularJS
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
 
The Art of AngularJS - DeRailed 2014
The Art of AngularJS - DeRailed 2014The Art of AngularJS - DeRailed 2014
The Art of AngularJS - DeRailed 2014
 
Angular.js опыт использования, проблемы и решения
Angular.js опыт использования, проблемы и решенияAngular.js опыт использования, проблемы и решения
Angular.js опыт использования, проблемы и решения
 
Angularjs
AngularjsAngularjs
Angularjs
 
Introduction to AJAX In WordPress
Introduction to AJAX In WordPressIntroduction to AJAX In WordPress
Introduction to AJAX In WordPress
 

Viewers also liked

Audit Informatique
Audit InformatiqueAudit Informatique
Audit Informatique
etienne
 
Etude des aspects de sécurité Android & Audit d'une application Android
Etude des aspects de sécurité Android & Audit d'une application AndroidEtude des aspects de sécurité Android & Audit d'une application Android
Etude des aspects de sécurité Android & Audit d'une application Android
Saad Dardar
 

Viewers also liked (6)

360View sécurité Business Objects
360View sécurité Business Objects360View sécurité Business Objects
360View sécurité Business Objects
 
JFTL2015 - Comment tester les performances ressenties par l’utilisateur d’une...
JFTL2015 - Comment tester les performances ressenties par l’utilisateur d’une...JFTL2015 - Comment tester les performances ressenties par l’utilisateur d’une...
JFTL2015 - Comment tester les performances ressenties par l’utilisateur d’une...
 
TDC 2014 - Arquitetura front-end com AngularJS
TDC 2014 - Arquitetura front-end com AngularJSTDC 2014 - Arquitetura front-end com AngularJS
TDC 2014 - Arquitetura front-end com AngularJS
 
Audit Informatique
Audit InformatiqueAudit Informatique
Audit Informatique
 
Etude des aspects de sécurité Android & Audit d'une application Android
Etude des aspects de sécurité Android & Audit d'une application AndroidEtude des aspects de sécurité Android & Audit d'une application Android
Etude des aspects de sécurité Android & Audit d'une application Android
 
Unpan016352
Unpan016352Unpan016352
Unpan016352
 

Similar to Angularjs Anti-patterns

AngularJS Introduction (Talk given on Aug 5 2013)
AngularJS Introduction (Talk given on Aug 5 2013)AngularJS Introduction (Talk given on Aug 5 2013)
AngularJS Introduction (Talk given on Aug 5 2013)
Abhishek Anand
 
angularjsmeetup-150303044616-conversion-gate01
angularjsmeetup-150303044616-conversion-gate01angularjsmeetup-150303044616-conversion-gate01
angularjsmeetup-150303044616-conversion-gate01
Teo E
 

Similar to Angularjs Anti-patterns (20)

Intro to AngularJs
Intro to AngularJsIntro to AngularJs
Intro to AngularJs
 
AngularJS Workshop
AngularJS WorkshopAngularJS Workshop
AngularJS Workshop
 
Optimizing a large angular application (ng conf)
Optimizing a large angular application (ng conf)Optimizing a large angular application (ng conf)
Optimizing a large angular application (ng conf)
 
Optimizing AngularJS Application
Optimizing AngularJS ApplicationOptimizing AngularJS Application
Optimizing AngularJS Application
 
Angular js 1.3 presentation for fed nov 2014
Angular js 1.3 presentation for fed   nov 2014Angular js 1.3 presentation for fed   nov 2014
Angular js 1.3 presentation for fed nov 2014
 
AngularJS Introduction (Talk given on Aug 5 2013)
AngularJS Introduction (Talk given on Aug 5 2013)AngularJS Introduction (Talk given on Aug 5 2013)
AngularJS Introduction (Talk given on Aug 5 2013)
 
Workshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte IIIWorkshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte III
 
angularjsmeetup-150303044616-conversion-gate01
angularjsmeetup-150303044616-conversion-gate01angularjsmeetup-150303044616-conversion-gate01
angularjsmeetup-150303044616-conversion-gate01
 
Angular js meetup
Angular js meetupAngular js meetup
Angular js meetup
 
Angular js
Angular jsAngular js
Angular js
 
Angular js
Angular jsAngular js
Angular js
 
Integrating AngularJS into the Campus CMS
Integrating AngularJS into the Campus CMSIntegrating AngularJS into the Campus CMS
Integrating AngularJS into the Campus CMS
 
Angular from Scratch
Angular from ScratchAngular from Scratch
Angular from Scratch
 
Everything You Need To Know About AngularJS
Everything You Need To Know About AngularJSEverything You Need To Know About AngularJS
Everything You Need To Know About AngularJS
 
AngularJS in practice
AngularJS in practiceAngularJS in practice
AngularJS in practice
 
Angular presentation
Angular presentationAngular presentation
Angular presentation
 
Backbone/Marionette introduction
Backbone/Marionette introductionBackbone/Marionette introduction
Backbone/Marionette introduction
 
Tips from angular js users anonymous
Tips from angular js users anonymousTips from angular js users anonymous
Tips from angular js users anonymous
 
Angular js-crash-course
Angular js-crash-courseAngular js-crash-course
Angular js-crash-course
 
React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)
React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)
React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)
 

Recently uploaded

Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 

Recently uploaded (20)

top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
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...
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
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
 
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 ...
 
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
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
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?
 
Strategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptxStrategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptx
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 

Angularjs Anti-patterns

  • 2. The List ● Using $() instead of angular.element() ● Using jQuery to manipulate the DOM ● Looking at $scope.$$phase ● Not making ng-model an object ● Controllers doing too much or too bloated o Manipulating DOM or model/data o Throwing everything on $scope o Overusing $rootscope ● Not doing things the “Angular” way o Programing like you would in jQuery o Manipulating the view instead of the model o Not using Angular built-ins ($timeout, ng-show, ng-click, etc.) ● Not encapsulating 3rd party libraries in an Angular service
  • 3. $() vs. angular.element If jQuery is available, angular.element is an alias for the jQuery function. If jQuery is not available, angular.element delegates to Angular's built-in subset of jQuery, called "jQuery lite" or "jqLite."“ Thus, angular.element is $(), so just use angular.element. Side note: don’t wrap in $element in a directive in angular.element, it already is one. From the docs:
  • 4. jQuery DOM manipulation jQuery DOM manipulation isn’t detected by Angular’s dirty checker. Generating DOM with jQuery doesn’t create any Angular bindings. From the docs: Stop trying to use jQuery to modify the DOM… . Really. That includes adding elements, removing elements, retrieving their contents, showing and hiding them. Use built-in directives, or write your own where necessary, to do your DOM manipulation “
  • 5. $scope.$$phase For future-proofing reasons, you should not use $$phase “ From Angular: Typically $scope.$$phase is used to conditionally call $apply. Instead, move the $apply to where you know you’re outside Angular. If nothing else, use $timeout. Code example
  • 6. ng-model not an object Code example If ng-model is a primitive type (string, number, boolean, null, undefined) any references in a child controller or directive won’t be doubly bound. If it’s a complex type (object or array), it will be doubly bound. Always make ng-model an object.
  • 7. Controller Bloat Don’t just throw everything on $scope or you’ll end up with $scope soup. Think of $scope as the public interface from your view to your data. Only expose what you need in the view. This goes for $rootscope as well. Think of $rootscope as the global namespace, and we all know not to pollute the global namespace.
  • 8. Controller Bloat Controllers should be small and lean. Things that don’t belong in a controller: ● DOM manipulation (belongs in a directive) ● Ajax or $http (belongs in a service) ● Model/data (belongs in a service) ● Model/data manipulation (belongs in the same service as the data) ● Business logic (belongs in a service) Exercise: convert bloated controller to lean controller
  • 9. The “Angular” Way Exercise: convert jQuery to Angular Programing in Angular is very different than programming in jQuery When using Angular, try not to manipulate the view (DOM), instead manipulate the model (data and how it is presented) Don’t forget about all the Angular built-in goodies like ng-show, ng-click, etc.
  • 10. 3rd Party Libraries When using 3rd party libraries, always wrap them in an Angular service. angular.module(‘myApp’).factory(‘_’, function() { return window._; }); angular.module(‘myApp’).controller(‘myCtrl’, function(_) { // can now use library as _ });
  • 11. 3rd Party Libraries By wrapping the library, you now allow Angular’s dependency injection to handle everything. This also makes testing easier as you can now inject a mock of the library into the unit test.

Editor's Notes

  1. Lean controller answer: http://jsfiddle.net/straker/t8dLzws3/
  2. Angular answer: http://jsfiddle.net/straker/kL5wdnbr/