- Use
controllerAssyntax instead of injecting$scope-- only inject$scopeinto controllers when needed e.g. when establishing watchers or listeners - Avoid unnecessary dependency injections
- Avoid using
$rootScopevariables/events - Avoid adding to the digest cycle
- Prefer
ng-bindattribute over{{}} - Use
ng-attr-x="{{vm.myVar}}", neverx={{vm.myVar}} - Use
ng-cloakto prevent all remaining flashes of unresolved template - Use
ng-strict-dito introduce strict dependency checking - Prefer
ng-ifoverng-show/ng-hide - Use
track byclause on anyng-repeatthat 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 strictprologue - Avoid invoking expensive functions in template expressions -- these are reevaluated frequently
- Never use
$rootScope.$onin 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
mainidiom 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