GitHub - toddmotto/angularjs-styleguide at angular-old-es5 (original) (raw)

Angular 1.4.x styleguide (ES5/old version)

For the new ES2015, component architecture guide please see

Opinionated Angular styleguide for teams by @toddmotto

A standardised approach for developing Angular applications in teams. This styleguide touches on concepts, syntax, conventions and is based on my experience writing, talking, and building Angular applications.

Join the Ultimate AngularJS experience and fully master basic and advanced Angular features

Community

John Papa and I have discussed in-depth styling patterns for Angular and as such have both released separate styleguides. Thanks to those discussions, I've learned some great tips from John that have helped shape this guide. We've both created our own take on a styleguide. I urge you to check his out to compare thoughts.

See the original article that sparked this off

Table of Contents

  1. Modules
  2. Controllers
  3. Services and Factory
  4. Directives
  5. Filters
  6. Routing resolves
  7. Publish and subscribe events
  8. Performance
  9. Angular wrapper references
  10. Comment standards
  11. Minification and annotation

Modules

})();

Back to top

Controllers

}
Why? : Function context changes the this value, use it to avoid .bind() calls and scoping issues

}
// recommended
function MainCtrl () {

let doSomething = arg => {
console.log(this);
};

// exports
this.doSomething = doSomething;

}
Why? : Use ES6 arrow functions when necessary to access the this value lexically

}
// recommended
function MainCtrl (UserService) {
var vm = this;
UserService
.getUsers()
.then(function (response) {
vm.users = response;
});
vm.removeUser = function (user, index) {
UserService
.removeUser(user)
.then(function (response) {
vm.users.splice(index, 1);
});
};
}
Why? : Controllers should fetch Model data from Services, avoiding any Business logic. Controllers should act as a ViewModel and control the data flowing between the Model and the View presentational layer. Business logic in Controllers makes testing Services impossible.

Back to top

Services and Factory

Services: act as a constructor function and are instantiated with the new keyword. Use this for public methods and variables

```javascript
function SomeService () {
  this.someMethod = function () {

  };
}
angular
  .module('app')
  .service('SomeService', SomeService);

```

Factory: Business logic or provider modules, return an Object or closure

Back to top

Directives

Back to top

Filters

Back to top

Routing resolves

Back to top

Publish and subscribe events

Back to top

Performance

Back to top

Angular wrapper references

Back to top

Comment standards

Back to top

Minification and annotation

Back to top

Angular docs

For anything else, including API reference, check the Angular documentation.

Contributing

Open an issue first to discuss potential changes/additions.

License

(The MIT License)

Copyright (c) 2015-2016 Todd Motto

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.