Skip to content

Latest commit

 

History

History
85 lines (66 loc) · 4.14 KB

Angular-best-pratices---Styles-guides.md

File metadata and controls

85 lines (66 loc) · 4.14 KB

Official style guide

Best pratices documents

General Pitfalls

Using promises

Scope Pitfalls

Share code between controller / "the controller as" syntax

Rules for the Controller going forward:

  • Controllers should hold zero logic
  • Controllers should bind references to Models only (and call methods returned from promises)
  • Controllers only bring logic together
  • Controller drives Model changes, and View changes. Keyword; drives, not creates/persists, it triggers them!
  • Delegate updating of logic inside Factories, don't resolve data inside a Controller, only update the Controller's value with updated Factory logic, this avoids repeated code across Controllers as well as Factory tests made easier
  • Use "Controller as" (read the article)
  • Keep things simple, I prefer XXXXCtrl and XXXXFactory, I know exactly what the two do, we don't need fancy names for things
  • Keep method/prop names consistent across shared methods, such as this.something = MyFactory.something; otherwise it becomes confusing
  • Factories hold the Model, change, get, update, and persist the Model changes
  • Think about the Factory as an Object that you need to persist, rather than persisting inside a Controller
  • Talk to other Factories inside your Factory, keep them out the Controller (things like success/error handling)
  • Try to avoid injecting $scope into Controllers, generally there are better ways to do what you need, such as avoiding $scope.$watch()

Minification syntax

For dependency injection, it's a good pratice to use the array [$scope, $log, function($scope, $log syntax

http://stackoverflow.com/questions/16055449/angularjs-minification-issue-in-directive

this vs scope

Modularization

Module definition

http://henriquat.re/modularizing-angularjs/modularizing-angular-applications/modularizing-angular-applications.html Pollute global variable space

var mod = angular.module("foo", []);
mod.directive("directiveA", function() { /* ... */ });
mod.directive("directiveB", function() { /* ... */ });
mod.directive("directiveC", function() { /* ... */ });

chainable structure don't pollute global space

angular.module("foo", [])
   .directive("directiveA", function() { /* ... */ })
   .directive("directiveB", function() { /* ... */ })
   .directive("directiveC", function() { /* ... */ });

If the definition of the module need to be updated in other file

angular.module("name").directive...

Without redefining the depency array

module splitting

Split module by functionallity rather than by type (inbox, composer....) rather than (services, controllers....)