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

PPTX
Top 10 Mistakes AngularJS Developers Make
PPTX
Angularjs Basics
PDF
AngularJS best-practices
PPTX
AngularJS Beginners Workshop
PPTX
Why angular js Framework
PPTX
Practical AngularJS
PPTX
AngularJS in 60ish Minutes
PPTX
Angular js architecture (v1.4.8)
Top 10 Mistakes AngularJS Developers Make
Angularjs Basics
AngularJS best-practices
AngularJS Beginners Workshop
Why angular js Framework
Practical AngularJS
AngularJS in 60ish Minutes
Angular js architecture (v1.4.8)

What's hot (20)

PDF
Introduction to AngularJS
PDF
Advanced Tips & Tricks for using Angular JS
PPTX
Building an End-to-End AngularJS Application
PDF
Angular js - the beginning
ODP
AngularJs Crash Course
PDF
AngularJS - What is it & Why is it awesome ? (with demos)
PPTX
Angular js for beginners
PDF
AngularJS Framework
PPTX
Angularjs Performance
PPTX
AngularJs presentation
PPTX
AngularJS Best Practices
PPTX
Introduction to Angularjs
PPTX
AngularJS intro
PPTX
AngularJs
DOCX
Filters in AngularJS
PPTX
AngularJS Directives
PDF
The Art of AngularJS - DeRailed 2014
PDF
Angular.js опыт использования, проблемы и решения
ODP
Angularjs
PDF
Introduction to AJAX In WordPress
Introduction to AngularJS
Advanced Tips & Tricks for using Angular JS
Building an End-to-End AngularJS Application
Angular js - the beginning
AngularJs Crash Course
AngularJS - What is it & Why is it awesome ? (with demos)
Angular js for beginners
AngularJS Framework
Angularjs Performance
AngularJs presentation
AngularJS Best Practices
Introduction to Angularjs
AngularJS intro
AngularJs
Filters in AngularJS
AngularJS Directives
The Art of AngularJS - DeRailed 2014
Angular.js опыт использования, проблемы и решения
Angularjs
Introduction to AJAX In WordPress
Ad

Viewers also liked (6)

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

Similar to Angularjs Anti-patterns (20)

PPTX
Intro to AngularJs
PDF
AngularJS Workshop
PPTX
Optimizing a large angular application (ng conf)
PDF
Optimizing AngularJS Application
PPTX
Angular js 1.3 presentation for fed nov 2014
PPTX
AngularJS Introduction (Talk given on Aug 5 2013)
PDF
Workshop 14: AngularJS Parte III
PPT
Angular js meetup
PPT
angularjsmeetup-150303044616-conversion-gate01
PPTX
Angular js
PPTX
Angular js
PDF
Integrating AngularJS into the Campus CMS
PDF
Angular from Scratch
PDF
Everything You Need To Know About AngularJS
PDF
AngularJS in practice
PDF
Angular presentation
PDF
Tips from angular js users anonymous
ODP
Angular js-crash-course
PPTX
React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)
PPTX
Training On Angular Js
Intro to AngularJs
AngularJS Workshop
Optimizing a large angular application (ng conf)
Optimizing AngularJS Application
Angular js 1.3 presentation for fed nov 2014
AngularJS Introduction (Talk given on Aug 5 2013)
Workshop 14: AngularJS Parte III
Angular js meetup
angularjsmeetup-150303044616-conversion-gate01
Angular js
Angular js
Integrating AngularJS into the Campus CMS
Angular from Scratch
Everything You Need To Know About AngularJS
AngularJS in practice
Angular presentation
Tips from angular js users anonymous
Angular js-crash-course
React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)
Training On Angular Js

Recently uploaded (20)

PPTX
Introduction to Artificial Intelligence
DOCX
The Five Best AI Cover Tools in 2025.docx
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Complete React Javascript Course Syllabus.pdf
PDF
System and Network Administration Chapter 2
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
top salesforce developer skills in 2025.pdf
PDF
System and Network Administraation Chapter 3
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPT
Introduction Database Management System for Course Database
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Digital Strategies for Manufacturing Companies
Introduction to Artificial Intelligence
The Five Best AI Cover Tools in 2025.docx
Operating system designcfffgfgggggggvggggggggg
Softaken Excel to vCard Converter Software.pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Complete React Javascript Course Syllabus.pdf
System and Network Administration Chapter 2
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
2025 Textile ERP Trends: SAP, Odoo & Oracle
How to Choose the Right IT Partner for Your Business in Malaysia
top salesforce developer skills in 2025.pdf
System and Network Administraation Chapter 3
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Upgrade and Innovation Strategies for SAP ERP Customers
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Introduction Database Management System for Course Database
Odoo POS Development Services by CandidRoot Solutions
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Digital Strategies for Manufacturing Companies

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

  • #9: Lean controller answer: http://jsfiddle.net/straker/t8dLzws3/
  • #10: Angular answer: http://jsfiddle.net/straker/kL5wdnbr/