Marc Hughes


Home
Blog
Twitter
LinkedIn
GitHub
about
I am a developer from a bit west of Boston.

Angular JS rules of thumb

26 Oct 2015

Here are a few things I've concluded about developing in Angular JS over the past year -

Prefer events over watchers. When you need a bit of code to execute when something else happens, it's almost always better to use an event over a watcher. It's slightly more complicated, but if you plan for that early, your app will be a lot faster.

Good:

scope.$on('cardChanged', doSomething);  
...  
scope.card.name = "hello"  
scope.$emit('cardChanged', scope.card);

Bad:

scope.$watch('card', doSomething, true);  
...  
scope.card.name = "hello"

Extensively use ui.router

Seriously, don't bother with the built in router. Use this one, use it extensively, and your life will be easier.

https://github.com/angular-ui/ui-router

Modules

Separate out your reusable components into modules.

Services

Push as much business logic into your services as you can. Have your controllers deal with the UI bits, but the service do the actual work. Makes it a lot easier to write tests for the important bits and lets you reuse code more often.