Skip to content

Instantly share code, notes, and snippets.

@malexandre
Last active January 25, 2016 16:45
Show Gist options
  • Save malexandre/47171098cdc279f03f04 to your computer and use it in GitHub Desktop.
Save malexandre/47171098cdc279f03f04 to your computer and use it in GitHub Desktop.
AngularJS Convention
(function()
{
'use strict';
angular
.module('Controllers')
.controller('MyController', MyController);
/* @ngInject */
function MyController($scope, MyService)
{
var vm = this;
// Private members
var example = 'example';
// Public API
vm.example = example;
// Public members
vm.member = 'example';
// Public services
vm.MyService = MyService;
$scope.$watch('value', manageValue);
init();
////////////
function example(_example)
{
example = _example;
}
function init()
{
// Nothing to do
}
function manageValue(newValue, oldValue)
{
// Nothing to do
}
}
})();
(function()
{
'use strict';
angular
.module('Directives')
.directive('myDirective', myDirective);
/* @ngInject */
function myDirective()
{
return {
restrict: 'E',
templateUrl: '/my/template/path.html',
scope: {
example: '=lxExample'
},
link: link,
controller: MyController,
controllerAs: 'vm',
bindToController: true, // because the scope is isolated
replace: true,
transclude: true
};
function link(scope, element, attrs, ctrl)
{
// Nothing to do
}
}
/* @ngInject */
function MyController()
{
var vm = this;
}
})();
(function()
{
'use strict';
angular
.module('Services')
.service('MyService', MyService);
/* @ngInject */
function MyService()
{
var service = this;
// Private members
var privateMember = true;
// Public API
service.example = example;
// Public members
service.member = 'example';
init();
////////////
function example()
{
console.log('Example');
}
function init()
{
// Nothing to do
}
}
})();
@malexandre
Copy link
Author

All methods, public or private, are together by alphabetical order.
Public members are declared by alphabetical order.
Services are declared by alphabetical order.
Private members are declared by alphabetical order, except "var service" or "var vm".

The Angular Dependency Injection list starts by the $, and then the non-$. Each value is ordered by alphabetical order.

Avoid declaring inline anonymous function. It forces you to name the function, and the function gets available.

Inspired by https://github.com/johnpapa/angular-styleguide

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment