appirio-tech/angularjs-styleguide

Name: angularjs-styleguide

Owner: Topcoder

Description: AngularJS Style Guide: A starting point for AngularJS development teams to provide consistency through good practices.

Created: 2015-02-18 22:20:03.0

Updated: 2015-05-06 21:28:05.0

Pushed: 2015-12-23 13:44:42.0

Homepage: http://johnpapa.net

Size: 2914

Language: null

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

AngularJS Style Guide

Much of this comes from a fork of John Papa's AngularJS Styleguide (specifically, the section called “Angular Style Guide (after John Papa). But we'll open with some general guidelines.

General Guidelines

Approved AngularJS libraries
Other guidelines
See the Styles in a Sample App

While this guide explains the what, why and how, I find it helpful to see them in practice. This guide is accompanied by a sample application that follows these styles and patterns. You can find the sample application (named modular) here in the modular folder. Feel free to grab it, clone it, or fork it. Instructions on running it are in its readme.

Angular Style Guide (after John Papa)

Table of Contents
  1. Single Responsibility
  2. IIFE
  3. Modules
  4. Controllers
  5. Services
  6. Factories
  7. Data Services
  8. Directives
  9. Resolving Promises for a Controller
  10. Manual Annotating for Dependency Injection
  11. Minification and Annotation
  12. Exception Handling
  13. Naming
  14. Application Structure LIFT Principle
  15. Application Structure
  16. Modularity
  17. Startup Logic
  18. Angular $ Wrapper Services
  19. Testing
  20. Animations
  21. Comments
  22. JSHint
  23. Constants
  24. File Templates and Snippets
  25. Yeoman Generator
  26. Routing
  27. Task Automation
  28. AngularJS Docs
  29. Contributing
  30. License
Single Responsibility
Rule of 1
[Style Y001]

Back to top

IIFE
JavaScript Closures
[Style Y010]

Back to top

Modules
Avoid Naming Collisions
[Style Y020]
Definitions (aka Setters)
[Style Y021]
Getters
[Style Y022]
Setting vs Getting
[Style Y023]
Named vs Anonymous Functions
[Style Y024]

Back to top

Controllers
controllerAs View Syntax
[Style Y030]
controllerAs Controller Syntax
[Style Y031]
controllerAs with vm
[Style Y032]
Bindable Members Up Top
[Style Y033]
Function Declarations to Hide Implementation Details
[Style Y034]
Defer Controller Logic
[Style Y035]
Keep Controllers Focused
[Style Y037]
Assigning Controllers
[Style Y038]

Back to top

Services
Singletons
[Style Y040]

Back to top

Factories
Single Responsibility
[Style Y050]
Singletons
[Style Y051]
Accessible Members Up Top
[Style Y052]
Function Declarations to Hide Implementation Details
[Style Y053]

Back to top

Data Services
Separate Data Calls
[Style Y060]
Return a Promise from Data Calls
[Style Y061]
Directives
Limit 1 Per File
[Style Y070]
Manipulate DOM in a Directive
[Style Y072]
Provide a Unique Directive Prefix
[Style Y073]
Restrict to Elements and Attributes
[Style Y074]
Directives and ControllerAs
[Style Y075] [Style Y076]

Back to top

Resolving Promises for a Controller
Controller Activation Promises
[Style Y080]
Route Resolve Promises
[Style Y081]

Back to top

Manual Annotating for Dependency Injection
UnSafe from Minification
[Style Y090]
Manually Identify Dependencies
[Style Y091]
Manually Identify Route Resolver Dependencies
[Style Y092]

Back to top

Minification and Annotation
ng-annotate
[Style Y100]
Use Gulp or Grunt for ng-annotate
[Style Y101]

Back to top

Exception Handling
decorators
[Style Y110]
Exception Catchers
[Style Y111]
Route Errors
[Style Y112]

Back to top

Naming
Naming Guidelines
[Style Y120]
Feature File Names
[Style Y121]
Test File Names
[Style Y122]
Controller Names
[Style Y123]
Controller Name Suffix
[Style Y124]
Factory Names
[Style Y125]
Directive Component Names
[Style Y126]
Modules
[Style Y127]
Configuration
[Style Y128]
Routes
[Style Y129]

Back to top

Application Structure LIFT Principle
LIFT
[Style Y140]
Locate
[Style Y141]
Identify
[Style Y142]
Flat
[Style Y143]
T-DRY (Try to Stick to DRY)
[Style Y144]

Back to top

Application Structure
Overall Guidelines
[Style Y150]
Layout
[Style Y151]
Folders-by-Feature Structure
[Style Y152]

Back to top

Modularity
Many Small, Self Contained Modules
[Style Y160]
Create an App Module
[Style Y161]
Keep the App Module Thin
[Style Y162]
Feature Areas are Modules
[Style Y163]
Reusable Blocks are Modules
[Style Y164]
Module Dependencies
[Style Y165]

Back to top

Startup Logic
Configuration
[Style Y170]
Run Blocks
[Style Y171]

Back to top

Angular $ Wrapper Services
$document and $window
[Style Y180]
$timeout and $interval
[Style Y181]

Back to top

Testing

Unit testing helps maintain clean code, as such I included some of my recommendations for unit testing foundations with links for more information.

Write Tests with Stories
[Style Y190]
Testing Library
[Style Y191]
Test Runner
[Style Y192]
Stubbing and Spying
[Style Y193]
Headless Browser
[Style Y194]
Code Analysis
[Style Y195]
Alleviate Globals for JSHint Rules on Tests
[Style Y196]
Organizing Tests
[Style Y197]

Back to top

Animations
Usage
[Style Y210]
Sub Second
[Style Y211]
animate.css
[Style Y212]

Back to top

Comments
jsDoc
[Style Y220]

Back to top

JS Hint
Use an Options File
[Style Y230]

Back to top

Constants
Vendor Globals
[Style Y240] [Style Y241]

Back to top

File Templates and Snippets

Use file templates or snippets to help follow consistent styles and patterns. Here are templates and/or snippets for some of the web development editors and IDEs.

Sublime Text
[Style Y250]
Visual Studio
[Style Y251]
WebStorm
[Style Y252]
Atom
[Style Y253]

Back to top

Yeoman Generator
[Style Y260]

You can use the HotTowel yeoman generator to create an app that serves as a starting point for Angular that follows this style guide.

  1. Install generator-hottowel

    install -g generator-hottowel
    
  2. Create a new folder and change directory to it

    r myapp
    yapp
    
  3. Run the generator

    ottowel helloWorld
    

Back to top

Routing

Client-side routing is important for creating a navigation flow between views and composing views that are made of many smaller templates and directives.

[Style Y270] [Style Y271]

Back to top

Task Automation

Use Gulp or Grunt for creating automated tasks. Gulp leans to code over configuration while Grunt leans to configuration over code. I personally prefer Gulp as I feel it is easier to read and write, but both are excellent.

[Style Y400]

Back to top

AngularJS docs

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

Contributing

Open an issue first to discuss potential changes/additions. If you have questions with the guide, feel free to leave them as issues in the repository. If you find a typo, create a pull request. The idea is to keep the content up to date and use github?s native feature to help tell the story with issues and PR?s, which are all searchable via google. Why? Because odds are if you have a question, someone else does too! You can learn more here at about how to contribute.

By contributing to this repository you are agreeing to make your content available subject to the license of this repository.

Process
1. Discuss the changes in a GitHub issue.
2. Open a Pull Request, reference the issue, and explain the change and why it adds value.
3. The Pull Request will be evaluated and either merged or declined.
License

tldr; Use this guide. Attributions are appreciated.

(The MIT License)

Copyright (c) 2014 John Papa

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.

Back to top


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.