SlideShare a Scribd company logo
Top 10 Mistakes AngularJS 
Developers Make 
Mark Meyer 
@nuclearghost
About Me 
Software Engineer at 
Sharethrough 
Developing on AngularJS since 
1.1.5 (triangle-squarification) 
Built ~40k line AngularJS app 
and many much smaller 
AngularJS apps
What’s the 
point? 
AngularJS === Awesome 
Easy to Start 
Learning Curve Eventually 
Ramps up 
My Experiences from 
building a large application
The Mistakes 
1. MVC Directory Structure 
2. Modules 
3. 
4. 
5. 
6. Batarang 
7. Too Many Watchers 
8. Scoping $scope’s 
9. Manual Testing 
Dependency Injection 
Controllers Bloat 
Services v Factory 10. Using jQuery
Dependency Injection 
Great Design Pattern for Testing and Parallelizing Development 
Not the prettiest in AngularJS 
var app = angular.module('app',[]); 
app.controller('MainCtrl', function($scope, $timeout){ 
$timeout(function(){ 
console.log($scope); 
}, 1000); 
});
Array Style 
+ Safe for minification 
- Extra in-line code 
app.controller('MainCtrl', ['$scope', '$timeout', function($scope, $timeout){ 
$timeout(function(){ 
console.log($scope); 
}, 1000); 
}]);
$inject 
Property attached to controller functions 
Not as convenient for in-lining 
Potentially cleaner without duplicate names at declaration 
var MainCtrl = function($scope, $timeout){ 
$timeout(function(){ 
console.log($scope); 
}, 1000); 
}; 
MainCtrl.$inject = ['$scope', ‘$timeout']; //or MainCtrl[‘$inject’] 
app.controller('MainCtrl', MainCtrl);
ng-annotate 
Automatically add array style notations 
Integrate into build process before minification 
Can be easily integrated with Grunt, Gulp, Browserify, Rails Assets 
Pipeline, and more 
https://github.com/olov/ng-annotate
Controller Bloat 
Limit Controllers to bartering 
between view and data model 
Data model should live in 
provider (service, factory, etc.) 
Controllers should have view-model 
for data binding
Controller Bloat Example 
deadbolt by Ed Carter 
Chrome extension for hashing 
passphrase to secure password 
Very clean, readable code 
Controllers get a little lengthy handling all 
the user events 
https://github.com/bittenbytailfly/deadbolt-password- 
generator-chrome-extension/ 
blob/master/extension/js/control 
lers.js
Service v Factory 
Provider, Factory, Service, Value, Constant 
All based on Provider 
All singletons 
Definitely over complicated 
Read the docs: https://docs.angularjs.org/guide/providers
Value 
Often used for storing primitives, e.g. key string values 
var myApp = angular.module('myApp', []); 
myApp.value('clientId', 'a12345654321x'); 
myApp.controller('DemoController', ['clientId', function DemoController(clientId) { 
this.clientId = clientId; 
}]);
Constant 
Values available at config time and run time 
Every AngularJS app has config phase, then run phase 
myApp.constant('planetName', 'Greasy Giant');
Service 
Returns new’d instance of object returned in service 
Cannot create primitives 
app.service('helloWorldService', function(){ 
this.hello = function() { 
return "Hello World"; 
}; 
}); 
Service'. We regret this and know that we'll be somehow punished for our misdeed. It's like we named one of our
Factory 
Can have dependencies 
Can return primitives or functions 
myApp.factory('apiToken', ['clientId', function apiTokenFactory(clientId) { 
var encrypt = function(data1, data2) { 
// NSA-proof encryption algorithm: 
return (data1 + ':' + data2).toUpperCase(); 
}; 
var secret = window.localStorage.getItem('myApp.secret'); 
var apiToken = encrypt(clientId, secret); 
return apiToken; 
}]);
Provider 
Closest to the metal 
Must implement $get method 
Available during config phase
Provider Example 
myApp.provider('unicornLauncher', function UnicornLauncherProvider() { 
var useTinfoilShielding = false; 
this.useTinfoilShielding = function(value) { 
useTinfoilShielding = !!value; 
}; 
this.$get = ["apiToken", function unicornLauncherFactory(apiToken) { 
return new UnicornLauncher(apiToken, useTinfoilShielding); 
}]; 
}); 
myApp.config(["unicornLauncherProvider", function(unicornLauncherProvider) { 
unicornLauncherProvider.useTinfoilShielding(true); 
}]);
Provider Family Conclusion
The Mistakes 
1. MVC Directory Structure 
2. Modules 
3. 
4. 
5. 
6. Batarang 
7. Too Many Watchers 
8. Scoping $scope’s 
9. Manual Testing 
Dependency Injection 
Controllers Bloat 
Services v Factory 10. Using jQuery
11. Memory Leaks 
Controllers and Directives should subscribe to: $scope.$on('$destroy', …) 
Remove handlers 
Clean up resources which won’t be garbage collected automatically
MVC Directory Structure 
templates/ 
_login.html 
_feed.html 
app/ 
app.js 
controllers/ 
LoginController.js 
FeedController.js 
directives/ 
FeedEntryDirective.js 
services/ 
LoginService.js 
FeedService.js 
filters/ 
CapatalizeFilter.js 
app/ 
app.js 
Feed/ 
_feed.html 
FeedController.js 
FeedEntryDirective.js 
FeedService.js 
Login/ 
_login.html 
LoginController.js 
LoginService.js 
Shared/ 
CapatalizeFilter.js
Modules 
Group code into bundles which are independent 
Directory structure should follow module design 
Configure routes per module 
Each module manages its own dependencies
Batarang 
Still under active-isn development 
Check out ng-inspector: http://ng-inspector.org/
Watchers 
If something doesn’t change, don’t watch it 
Bindonce directive is your friend 
https://github.com/Pasvaz/bindonce
Scoping $scopes 
Learn prototypical inheritance in javascript 
http://www.airpair.com/javascript/workshops/javascript-prototypal-inheritance 
Don’t stress too much 
2.0 is coming
Testing 
Come back tomorrow 
http://www.airpair.com/angularjs/ 
workshops/unit-testing-angularjs
Further Reading 
Angular Best Practices: https://github.com/angular/angular.js/wiki/Best- 
Practices 
Officical App Structure Recommendation: 
https://docs.google.com/document/d/1XXMvReO8- 
Awi1EZXAXS4PzDzdNvV6pGcuaF4Q9821Es/pub
Angular 2.0 is coming! 
Major changes in the pipeline 
Goodbye Controllers, $scope, angular.module, jqLite 
Rob Eisenberg goes in depth: http://eisenbergeffect.bluespire.com/all-about- 
angular-2-0/ 
Igor Minar and Tobias Bosch overview: 
https://docs.google.com/presentation/d/1XQP0_NTzCUcFweauLlkZpbbh 
NVYbYy156oD--KLmXsk/edit#slide=id.g498335282_0112
Wrap Up 
Reach me on twitter: @nuclearghost 
Want individualized help?: http://airpair.me/NuclearGhost 
Thanks for your time!

More Related Content

PPTX
Angularjs Anti-patterns
PPTX
Angularjs Basics
PDF
AngularJS best-practices
PPTX
Why angular js Framework
PPTX
AngularJS Beginners Workshop
PPTX
Practical AngularJS
PDF
Advanced Tips & Tricks for using Angular JS
PPTX
AngularJS in 60ish Minutes
Angularjs Anti-patterns
Angularjs Basics
AngularJS best-practices
Why angular js Framework
AngularJS Beginners Workshop
Practical AngularJS
Advanced Tips & Tricks for using Angular JS
AngularJS in 60ish Minutes

What's hot (20)

PPTX
Angular js architecture (v1.4.8)
PDF
Introduction to AngularJS
ODP
AngularJs Crash Course
PDF
Angular js - the beginning
PDF
AngularJS - What is it & Why is it awesome ? (with demos)
PDF
AngularJS Framework
PPTX
AngularJs presentation
PPTX
Building an End-to-End AngularJS Application
PPTX
Introduction to Angularjs
PPTX
Angularjs Performance
PPTX
Angular js for beginners
PPTX
AngularJS Best Practices
PPTX
AngularJS intro
PPTX
AngularJs
PDF
The Art of AngularJS - DeRailed 2014
ODP
Angularjs
PPTX
AngularJS for Java Developers
DOCX
Filters in AngularJS
PPTX
AngularJS Directives
PDF
Introduction to AJAX In WordPress
Angular js architecture (v1.4.8)
Introduction to AngularJS
AngularJs Crash Course
Angular js - the beginning
AngularJS - What is it & Why is it awesome ? (with demos)
AngularJS Framework
AngularJs presentation
Building an End-to-End AngularJS Application
Introduction to Angularjs
Angularjs Performance
Angular js for beginners
AngularJS Best Practices
AngularJS intro
AngularJs
The Art of AngularJS - DeRailed 2014
Angularjs
AngularJS for Java Developers
Filters in AngularJS
AngularJS Directives
Introduction to AJAX In WordPress
Ad

Similar to Top 10 Mistakes AngularJS Developers Make (20)

PPTX
Angular Js Basics
PPTX
Valentine with AngularJS
PPTX
AngularJs Workshop SDP December 28th 2014
PPTX
Angular JS deep dive
PPTX
angularJs Workshop
PPTX
Angular js
PDF
Introduction to AngularJS
PPTX
Front end development with Angular JS
PDF
Mini-Training: AngularJS
PDF
From Web App Model Design to Production with Wakanda
PPTX
Mean stack Magics
PPTX
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
PPTX
Intoduction to Angularjs
PDF
Workshop 13: AngularJS Parte II
PPTX
Building a dashboard using AngularJS
PPTX
Introduction to angular js for .net developers
PPT
Coffee@DBG - Exploring Angular JS
PPTX
AgularJS basics- angular directives and controllers
PPTX
Training On Angular Js
PDF
Karlsruher Entwicklertag 2013 - Webanwendungen mit AngularJS
Angular Js Basics
Valentine with AngularJS
AngularJs Workshop SDP December 28th 2014
Angular JS deep dive
angularJs Workshop
Angular js
Introduction to AngularJS
Front end development with Angular JS
Mini-Training: AngularJS
From Web App Model Design to Production with Wakanda
Mean stack Magics
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
Intoduction to Angularjs
Workshop 13: AngularJS Parte II
Building a dashboard using AngularJS
Introduction to angular js for .net developers
Coffee@DBG - Exploring Angular JS
AgularJS basics- angular directives and controllers
Training On Angular Js
Karlsruher Entwicklertag 2013 - Webanwendungen mit AngularJS
Ad

Recently uploaded (20)

PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
L1 - Introduction to python Backend.pptx
PPTX
Essential Infomation Tech presentation.pptx
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
System and Network Administration Chapter 2
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Which alternative to Crystal Reports is best for small or large businesses.pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Understanding Forklifts - TECH EHS Solution
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Wondershare Filmora 15 Crack With Activation Key [2025
PTS Company Brochure 2025 (1).pdf.......
Upgrade and Innovation Strategies for SAP ERP Customers
How to Migrate SBCGlobal Email to Yahoo Easily
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Design an Analysis of Algorithms I-SECS-1021-03
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
L1 - Introduction to python Backend.pptx
Essential Infomation Tech presentation.pptx
wealthsignaloriginal-com-DS-text-... (1).pdf
2025 Textile ERP Trends: SAP, Odoo & Oracle
CHAPTER 2 - PM Management and IT Context
System and Network Administration Chapter 2

Top 10 Mistakes AngularJS Developers Make

  • 1. Top 10 Mistakes AngularJS Developers Make Mark Meyer @nuclearghost
  • 2. About Me Software Engineer at Sharethrough Developing on AngularJS since 1.1.5 (triangle-squarification) Built ~40k line AngularJS app and many much smaller AngularJS apps
  • 3. What’s the point? AngularJS === Awesome Easy to Start Learning Curve Eventually Ramps up My Experiences from building a large application
  • 4. The Mistakes 1. MVC Directory Structure 2. Modules 3. 4. 5. 6. Batarang 7. Too Many Watchers 8. Scoping $scope’s 9. Manual Testing Dependency Injection Controllers Bloat Services v Factory 10. Using jQuery
  • 5. Dependency Injection Great Design Pattern for Testing and Parallelizing Development Not the prettiest in AngularJS var app = angular.module('app',[]); app.controller('MainCtrl', function($scope, $timeout){ $timeout(function(){ console.log($scope); }, 1000); });
  • 6. Array Style + Safe for minification - Extra in-line code app.controller('MainCtrl', ['$scope', '$timeout', function($scope, $timeout){ $timeout(function(){ console.log($scope); }, 1000); }]);
  • 7. $inject Property attached to controller functions Not as convenient for in-lining Potentially cleaner without duplicate names at declaration var MainCtrl = function($scope, $timeout){ $timeout(function(){ console.log($scope); }, 1000); }; MainCtrl.$inject = ['$scope', ‘$timeout']; //or MainCtrl[‘$inject’] app.controller('MainCtrl', MainCtrl);
  • 8. ng-annotate Automatically add array style notations Integrate into build process before minification Can be easily integrated with Grunt, Gulp, Browserify, Rails Assets Pipeline, and more https://github.com/olov/ng-annotate
  • 9. Controller Bloat Limit Controllers to bartering between view and data model Data model should live in provider (service, factory, etc.) Controllers should have view-model for data binding
  • 10. Controller Bloat Example deadbolt by Ed Carter Chrome extension for hashing passphrase to secure password Very clean, readable code Controllers get a little lengthy handling all the user events https://github.com/bittenbytailfly/deadbolt-password- generator-chrome-extension/ blob/master/extension/js/control lers.js
  • 11. Service v Factory Provider, Factory, Service, Value, Constant All based on Provider All singletons Definitely over complicated Read the docs: https://docs.angularjs.org/guide/providers
  • 12. Value Often used for storing primitives, e.g. key string values var myApp = angular.module('myApp', []); myApp.value('clientId', 'a12345654321x'); myApp.controller('DemoController', ['clientId', function DemoController(clientId) { this.clientId = clientId; }]);
  • 13. Constant Values available at config time and run time Every AngularJS app has config phase, then run phase myApp.constant('planetName', 'Greasy Giant');
  • 14. Service Returns new’d instance of object returned in service Cannot create primitives app.service('helloWorldService', function(){ this.hello = function() { return "Hello World"; }; }); Service'. We regret this and know that we'll be somehow punished for our misdeed. It's like we named one of our
  • 15. Factory Can have dependencies Can return primitives or functions myApp.factory('apiToken', ['clientId', function apiTokenFactory(clientId) { var encrypt = function(data1, data2) { // NSA-proof encryption algorithm: return (data1 + ':' + data2).toUpperCase(); }; var secret = window.localStorage.getItem('myApp.secret'); var apiToken = encrypt(clientId, secret); return apiToken; }]);
  • 16. Provider Closest to the metal Must implement $get method Available during config phase
  • 17. Provider Example myApp.provider('unicornLauncher', function UnicornLauncherProvider() { var useTinfoilShielding = false; this.useTinfoilShielding = function(value) { useTinfoilShielding = !!value; }; this.$get = ["apiToken", function unicornLauncherFactory(apiToken) { return new UnicornLauncher(apiToken, useTinfoilShielding); }]; }); myApp.config(["unicornLauncherProvider", function(unicornLauncherProvider) { unicornLauncherProvider.useTinfoilShielding(true); }]);
  • 19. The Mistakes 1. MVC Directory Structure 2. Modules 3. 4. 5. 6. Batarang 7. Too Many Watchers 8. Scoping $scope’s 9. Manual Testing Dependency Injection Controllers Bloat Services v Factory 10. Using jQuery
  • 20. 11. Memory Leaks Controllers and Directives should subscribe to: $scope.$on('$destroy', …) Remove handlers Clean up resources which won’t be garbage collected automatically
  • 21. MVC Directory Structure templates/ _login.html _feed.html app/ app.js controllers/ LoginController.js FeedController.js directives/ FeedEntryDirective.js services/ LoginService.js FeedService.js filters/ CapatalizeFilter.js app/ app.js Feed/ _feed.html FeedController.js FeedEntryDirective.js FeedService.js Login/ _login.html LoginController.js LoginService.js Shared/ CapatalizeFilter.js
  • 22. Modules Group code into bundles which are independent Directory structure should follow module design Configure routes per module Each module manages its own dependencies
  • 23. Batarang Still under active-isn development Check out ng-inspector: http://ng-inspector.org/
  • 24. Watchers If something doesn’t change, don’t watch it Bindonce directive is your friend https://github.com/Pasvaz/bindonce
  • 25. Scoping $scopes Learn prototypical inheritance in javascript http://www.airpair.com/javascript/workshops/javascript-prototypal-inheritance Don’t stress too much 2.0 is coming
  • 26. Testing Come back tomorrow http://www.airpair.com/angularjs/ workshops/unit-testing-angularjs
  • 27. Further Reading Angular Best Practices: https://github.com/angular/angular.js/wiki/Best- Practices Officical App Structure Recommendation: https://docs.google.com/document/d/1XXMvReO8- Awi1EZXAXS4PzDzdNvV6pGcuaF4Q9821Es/pub
  • 28. Angular 2.0 is coming! Major changes in the pipeline Goodbye Controllers, $scope, angular.module, jqLite Rob Eisenberg goes in depth: http://eisenbergeffect.bluespire.com/all-about- angular-2-0/ Igor Minar and Tobias Bosch overview: https://docs.google.com/presentation/d/1XQP0_NTzCUcFweauLlkZpbbh NVYbYy156oD--KLmXsk/edit#slide=id.g498335282_0112
  • 29. Wrap Up Reach me on twitter: @nuclearghost Want individualized help?: http://airpair.me/NuclearGhost Thanks for your time!