20
Your Cloud. Your Business. Advanced Angular Tips and Tricks Jeremy Likness Principal Architect @ JeremyLikness

Advanced AngularJS Tips and Tricks

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

Page 1: Advanced AngularJS Tips and Tricks

Your Cloud.Your Business.

Advanced Angular Tips and Tricks Jeremy Likness Principal Architect @JeremyLikness

Page 2: Advanced AngularJS Tips and Tricks

www.ivision.com

Page 3: Advanced AngularJS Tips and Tricks

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

Page 4: Advanced AngularJS Tips and Tricks

BACKGROUND

Page 5: Advanced AngularJS Tips and Tricks

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…”?

Page 6: Advanced AngularJS Tips and Tricks

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

Page 7: Advanced AngularJS Tips and Tricks

DEMO: Lessons Learned

Page 8: Advanced AngularJS Tips and Tricks

INTEGRATION

Page 9: Advanced AngularJS Tips and Tricks

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

Page 10: Advanced AngularJS Tips and Tricks

DEMO: Integration

Page 11: Advanced AngularJS Tips and Tricks

BEST PRACTICES

Page 12: Advanced AngularJS Tips and Tricks

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

Page 13: Advanced AngularJS Tips and Tricks

DEMO: Best Practices

Page 14: Advanced AngularJS Tips and Tricks

DEBUGGING

Page 15: Advanced AngularJS Tips and Tricks

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

Page 16: Advanced AngularJS Tips and Tricks

DEMO: Debugging

Page 17: Advanced AngularJS Tips and Tricks

PERFORMANCE

Page 18: Advanced AngularJS Tips and Tricks

PERFORMANCE

• Batarang• Zone to instrument• Bindonce or new :: prefix • Double-list approach • ng-if vs. ng-show • Measure twice, cut once!

Page 19: Advanced AngularJS Tips and Tricks

DEMO: Performance

Page 20: Advanced AngularJS Tips and Tricks

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