Upload
jeremy-likness
View
1.340
Download
4
Tags:
Embed Size (px)
DESCRIPTION
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.
Citation preview
Your Cloud.Your Business.
Advanced Angular Tips and Tricks Jeremy Likness Principal Architect @JeremyLikness
www.ivision.com
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
BACKGROUND
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…”?
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
DEMO: Lessons Learned
INTEGRATION
INTEGRATION
• Register and reuse third-party components with Angular’s dependency injection system
• Wrap third-party components to trigger digest • Extend third-party components to be “Angular aware” • Wrap third-party controls with Angular directives • Create reusable libraries with Angular modules• Consume third-party modules with Angular
DEMO: Integration
BEST PRACTICES
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
DEMO: Best Practices
DEBUGGING
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
DEMO: Debugging
PERFORMANCE
PERFORMANCE
• Batarang• Zone to instrument• Bindonce or new :: prefix • Double-list approach • ng-if vs. ng-show • Measure twice, cut once!
DEMO: Performance
Questions?
Source for this deckhttps://github.com/JeremyLikness/AngularTipsAndTricks
Running example for this deckhttp://jeremylikness.github.io/AngularTipsAndTricks/
Mastering AngularJS Course: fundamentals, scope, digest, filters, dependency injection, services, web services, routes, directives, testing, debugging and performancehttps://www.wintellectnow.com/course/detail/mastering-angularjs
Jeremy Likness, Principal Architect @JeremyLikness