Chris J. Lee

Dallas Drupal Developer

You are here

My First Angular Application: Some lessons learned

For a project i'm doing at work. I'm utilizing a REST API to integrate with salesforce chatter and angularjs. The salesforce chatter api is rather simple and gets polled every minute or so; angular then gets updated based on the changes in the REST call.

This is probably my first real project utilizing angularjs. It's been pretty easy working with angularjs. There were several concepts i had to wrap my head around first: Angular Dependancy Injection, Angular patterns, and directives.

These three were some of the more challenging things i had to learn and grasp around.

Lessons learned

Controller creep

Some lessons i've learned from this first application is to utilize controllers parent / child controllers to organize scope. Easily, a simple application's controller can grow quickly and scope collisions are possible. - Use parent controllers when possible utilize parent controllers. Parent controllers can be used over $rootScope. I've yet found the need to utilize $rootScope to update other controllers - Everything is a scope Everything is a scope variable. When defining ng-models on you forms, beware of conflicting $scope. You might just implement accidently the same ng-model on a select without knowing and it might just override your $scope function. So make sure to define meaningful actions to your functions too.

Directives are your friend

  • Implement them Implement directives in order to abstract repetitive html templates or JavaScript functionality
  • Maintanence Directives allow for more maintainable and modular pieces of code. It makes it easier to read

$watch your back

  • $watch is not really necessary. They are expensive and i find the documentation confusing to understand. They often conflict with an existing $digest cycle when one implements a $watch
  • Understand how $watches are set If you learn what $watch's do then you'll more likely understand how things work beneath the syntactical sugaring layer of angular.
  • $watch vs $watchScope Once you figure out what the difference between these two; one would be more apt to understand the differences and how to trigger a $watch without needing to write a $watch.
© 2017 Chris J. Lee