- Use
controllerAs
syntax instead of injecting$scope
-- only inject$scope
into controllers when needed e.g. when establishing watchers or listeners - Avoid unnecessary dependency injections
- Avoid using
$rootScope
variables/events - Avoid adding to the digest cycle
- Prefer
ng-bind
attribute over{{}}
- Use
ng-attr-x="{{vm.myVar}}"
, neverx={{vm.myVar}}
- Use
ng-cloak
to prevent all remaining flashes of unresolved template - Use
ng-strict-di
to introduce strict dependency checking - Prefer
ng-if
overng-show
/ng-hide
- Use
track by
clause on anyng-repeat
that undergoes refresh - Avoid manipulating the DOM outside of directives
- Avoid heavy controllers -- put as much logic in services as possible, especially network logic
- Use promises to get data from services
- Use constructor style for your controller names (e.g.
HomeController
) - Lay out each file as an IIFE with the
use strict
prologue - Avoid invoking expensive functions in template expressions -- these are reevaluated frequently
- Never use
$rootScope.$on
in a controller unless accompanied by a corresponding$scope.$on('$destroy', myListener)
- Pass scope variables (as well as all other volatile data) by value when building requests
- Use function hoisting and the
main
idiom to organize source - Use resolver pattern to get data from services
- Structure app as a series of nested components, all the way up to the route templates/views