What happens when two third-party Angular modules export a service with the same name? How do you access scope or dependency injection from a third-party control? How can you always ensure asynchronous code is executed within a digest loop without having to check? Should you store commonly referenced variables in $rootScope or a service? How do you ensure promises are resolved before your controller is invoked? In this talk, Jeremy Likness covers advanced tips, tricks, and techniques for building Angular apps. Based on his hands-on experience building large scale enterprise Angular applications with distributed teams of over 20 developers authoring hundreds of services, controllers, filters, and directives across tens of thousands of lines of code, Jeremy shares common problems and straightforward solutions.
3. TODAY’S AGENDA
1. Background Where did these tips and tricks come from?
2. Integration Tips for integrating existing JavaScript code
3. Best Practices Lessons learned in the trenches
4. Debugging Debugging Angular apps
5. Performance Writing Angular apps that scale and perform well
5. BACKGROUND
• Several Enterprise AngularJS Projects
• Largest Project:
• 80,000+ lines of TypeScript code (generated JavaScript even bigger)
• 100s of controllers, services, filters, and directives
• Global team of 25+ developers
• 2 years of development before go-live
• Exceeded client expectations for performance in most areas
• How much content have you read began with “I was tinkering
with…” or “I was exploring…”?
6. Lessons Learned
• It’s important you pick a code structure. There is no “best one” but it is
important to have one. We did:
/controllers
../common
../products
../customers
/directives
../common
../products
• Decouple from $scope as fast as you can!
• Don’t rely on $scope hierarchy/inheritance
• Use services for communication
• Use controller as syntax
• Maxim of Least Angular
• Test Early, and Test Often!
• For large teams, consider TypeScript
12. BEST PRACTICES
• $scope (addressed earlier)
• Use values (or constants) where it makes sense
• Wrap APIs in services and understand interceptors
• Use $log and $exceptionHandler
• Apply behaviors by decorating existing services with $provide
• Define code using Angular’s extend
• Lazy-load components with the injector when they aren’t
commonly used
• Use route’s “resolve” to guarantee asynchronous context
15. DEBUGGING
• All major browsers provide F12 “debug tools”
• In Chrome, you can inspect/select an element and reference it as $0
• angular.element(x) wraps a DOM element as a node with Angular
functions e.g. angular.element($0)
• scope()
• isolateScope()
• controller()
• injector()
• Expand objects then navigate to source and set breakpoints/watches
• Batarang
20. Questions?
Source for this deck
https://github.com/JeremyLikness/AngularTipsAndTricks
Running example for this deck
http://jeremylikness.github.io/AngularTipsAndTricks/
Mastering AngularJS Course: fundamentals, scope, digest, filters, dependency injection,
services, web services, routes, directives, testing, debugging and performance
https://www.wintellectnow.com/course/detail/mastering-angularjs
Jeremy Likness, Principal Architect @JeremyLikness