Last active
January 25, 2016 16:45
-
-
Save malexandre/47171098cdc279f03f04 to your computer and use it in GitHub Desktop.
AngularJS Convention
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 | |
} | |
} | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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; | |
} | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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