
Introduction
The goal of this style guide is to present a set of best practices and style guidelines for one AngularJS application.
These best practices are collected from:
- AngularJS source code
- Source code or articles I've read
- My own experience
Note 1: this is still a draft of the style guide, its main goal is to be community-driven so filling the gaps will be greatly appreciated by the whole community.
Note 2: before following any of the guidelines in the translations of the English document, make sure they are up-to date. The latest version of the AngularJS style guide is in the current document.
In this style guide you won't find common guidelines for JavaScript development. Such can be found at:
- Google's JavaScript style guide
- Mozilla's JavaScript style guide
- Douglas Crockford's JavaScript style guide
- Airbnb JavaScript style guide
- Idiomatic JavaScript style guide
For AngularJS development recommended is the Google's JavaScript style guide.
In AngularJS's GitHub wiki there is a similar section by ProLoser, you can check it here.
Translations
Table of content
General
Directory structure
Since a large AngularJS application has many components it's best to structure it in a directory hierarchy.
There are two main approaches:
- Creating high-level divisions by component types and lower-level divisions by functionality.
In this way the directory structure will look like:
.
โโโ app
โย ย โโโ app.js
โย ย โโโ controllers
โย ย โย ย โโโ home
โย ย โย ย โย ย โโโ FirstCtrl.js
โย ย โย ย โย ย โโโ FirstCtrl.spec.js
โย ย โย ย โย ย โโโ SecondCtrl.js
โย ย โย ย โย ย โโโ SecondCtrl.spec.js
โย ย โย ย โโโ about
โย ย โย ย โโโ ThirdCtrl.js
โย ย โย ย โโโ ThirdCtrl.spec.js
โย ย โโโ directives
โย ย โย ย โโโ home
โย ย โย ย โย ย โโโ directive1.js
โย ย โย ย โย ย โโโ directive1.spec.js
โย ย โย ย โโโ about
โย ย โย ย โโโ directive2.js
โย ย โย ย โโโ directive2.spec.js
โย ย โย ย โโโ directive3.js
โย ย โย ย โโโ directive3.spec.js
โย ย โโโ filters
โย ย โย ย โโโ home
โย ย โย ย โโโ about
โย ย โโโ services
โย ย โโโ CommonService.js
โย ย โโโ CommonService.spec.js
โย ย โโโ cache
โย ย โย ย โโโ Cache1.js
โย ย โย ย โโโ Cache1.spec.js
โย ย โย ย โโโ Cache2.js
โย ย โย ย โโโ Cache2.spec.js
โย ย โโโ models
โย ย โโโ Model1.spec.js
โย ย โโโ Model1.js
โย ย โโโ Model2.spec.js
โย ย โโโ Model2.js
โโโ partials
โโโ lib
โโโ e2e-tests
- Creating high-level divisions by functionality and lower-level divisions by component types.
Here is its layout:
.
โโโ app
โย ย โโโ app.js
โย ย โโโ common
โย ย โย ย โโโ controllers
โย ย โย ย โโโ directives
โย ย โย ย โโโ filters
โย ย โย ย โโโ services
โย ย โโโ home
โย ย โย ย โโโ controllers
โย ย โย ย โย ย โโโ FirstCtrl.js
โย ย โย ย โย ย โโโ FirstCtrl.spec.js
โย ย โย ย โย ย โโโ SecondCtrl.js
โย ย โย ย โย ย โโโ SecondCtrl.spec.js
โย ย โย ย โโโ directives
โย ย โย ย โย ย โโโ directive1.js
โย ย โย ย โย ย โโโ directive1.spec.js
โย ย โย ย โโโ filters
โย ย โย ย โย ย โโโ filter1.js
โย ย โย ย โย ย โโโ filter1.spec.js
โย ย โย ย โย ย โโโ filter2.js
โย ย โย ย โย ย โโโ filter2.spec.js
โย ย โย ย โโโ services
โย ย โย ย โโโ service1.js
โย ย โย ย โโโ service1.spec.js
โย ย โย ย โโโ service2.js
โย ย โย ย โโโ service2.spec.js
โย ย โโโ about
โย ย โโโ controllers
โย ย โย ย โโโ ThirdCtrl.js
โย ย โย ย โโโ ThirdCtrl.spec.js
โย ย โโโ directives
โย ย โย ย โโโ directive2.js
โย ย โย ย โโโ directive2.spec.js
โย ย โย ย โโโ directive3.js
โย ย โย ย โโโ directive3.spec.js
โย ย โโโ filters
โย ย โย ย โโโ filter3.js
โย ย โย ย โโโ filter3.spec.js
โย ย โโโ services
โย ย โโโ service3.js
โย ย โโโ service3.spec.js
โโโ partials
โโโ lib
โโโ e2e-tests
- In case the directory name contains multiple words, use lisp-case syntax:
app
โโโ app.js
โโโ my-complex-module
ย ย โโโ controllers
ย ย โโโ directives
ย ย โโโ filters
ย ย โโโ services
- Put all the files associated with the given directive (i.e. templates, CSS/SASS files, JavaScript) in a single folder. If you choose to use this style be consistent and use it everywhere along your project.
app
โโโ directives
โโโ directive1
โย ย โโโ directive1.html
โย ย โโโ directive1.js
โย ย โโโ directive1.spec.js
โย ย โโโ directive1.sass
โโโ directive2
โโโ directive2.html
โโโ directive2.js
โโโ directive2.spec.js
โโโ directive2.sass
This approach can be combined with both directory structures above.
- The unit tests for a given component (
*.spec.js) should be located in the directory where the component is. This way when you make changes to a given component finding its test is easy. The tests also act as documentation and show use cases.
services
โโโ cache
โย ย โโโ cache1.js
โย ย โโโ cache1.spec.js
โโโ models
โโโ model1.js
โโโ model1.spec.js
- The
app.js file should contain route definitions, configuration and/or manual bootstrap (if required).
- Each JavaScript file should only hold a single component. The file should be named with the component's name.
- Use AngularJS project structure template like Yeoman, ng-boilerplate.
Conventions about component naming can be found in each component section.
Markup
TLDR; Put the scripts at the bottom.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>MyApp</title>
</head>
<body>
<div ng-app="myApp">
<div ng-view></div>
</div>
<script src="angular.js"></script>
<script src="app.js"></script>
</body>
</html>
Keep things simple and put AngularJS specific directives after standard attributes. This will make it easier to skim your code and will make it easier to maintain because your attributes are consistently grouped and positioned.
<form class="frm" ng-submit="login.authenticate()">
<div>
<input class="ipt" type="text" placeholder="name" require ng-model="user.name">
</div>
</form>
Other HTML attributes should follow the Code Guide's recommendation
Naming conventions
The following table is shown the naming conventions for every element:
| Element |
Naming style |
Example |
usage |
| Modules |
lowerCamelCase |
angularApp |
|
| Controllers |
Functionality + 'Ctrl' |
AdminCtrl |
|
| Directives |
lowerCamelCase |
userInfo |
|
| Filters |
lowerCamelCase |
userFilter |
|
| Services |
UpperCamelCase |
User |
constructor |
| Factories |
lowerCamelCase |
dataFactory |
others |
Others
- Use:
$timeout instead of setTimeout
$interval instead of setInterval
$window instead of window
$document instead of document
$http instead of $.ajax
$location instead of window.location or $window.location
$cookies instead of document.cookie
This will make your testing easier and in some cases prevent unexpected behaviour (for example, if you missed $scope.$apply in setTimeout).
Automate your workflow using tools like:
Use promises ($q) instead of callbacks. It will make your code look more elegant and clean, and save you from callback hell.
Use $resource instead of $http when possible. The higher level of abstraction will save you from redundancy.
Use an AngularJS pre-minifier (ng-annotate) for preventing problems after minification.
Don't use globals. Resolve all dependencies using Dependency Injection, this will prevent bugs and monkey patching when testing.
Avoid globals by using Grunt/Gulp to wrap your code in Immediately Invoked Function Expression (IIFE). You can use plugins like grunt-wrap or gulp-wrap for this purpose. Example (using Gulp)
gulp.src("./src/*.js")
.pipe(wrap('(function(){\n"use strict";\n<%= contents %>\n})();'))
.pipe(gulp.dest("./dist"));
Do not pollute your $scope. Only add functions and variables that are being used in the templates.
Prefer the usage of controllers instead of ngInit. There are only a few appropriate uses of ngInit, such as for aliasing special properties of ngRepeat, and for injecting data via server side scripting. Besides these few cases, you should use controllers rather than ngInit to initialize values on a scope. The expression passed to ngInit should go through lexing, parsing and evaluation by the Angular interpreter implemented inside the $parse service. This leads to: