con un clic
angularjs
// Google's official AngularJS style guide. Covers controllers, services, directives, modules, dependency injection, and AngularJS 1.x best practices.
// Google's official AngularJS style guide. Covers controllers, services, directives, modules, dependency injection, and AngularJS 1.x best practices.
| name | angularjs |
| description | Google's official AngularJS style guide. Covers controllers, services, directives, modules, dependency injection, and AngularJS 1.x best practices. |
Official Google AngularJS coding standards for consistent Angular 1.x applications.
Note: This guide covers AngularJS (Angular 1.x). For modern Angular (2+), see the Angular style guide.
$scope when possible$inject$rootScope — use services for shared state| Element | Convention | Example |
|---|---|---|
| Modules | lowerCamelCase | myApp, myApp.users |
| Controllers | UpperCamelCase + Ctrl | UserCtrl, HomeCtrl |
| Services/Factories | UpperCamelCase | UserService, AuthFactory |
| Directives | lowerCamelCase | myDirective, userCard |
| Filters | lowerCamelCase | dateFormat, currencyDisplay |
| Files | feature.type.js | user.controller.js |
// ✓ CORRECT - controllerAs syntax
angular.module('myApp')
.controller('UserCtrl', UserCtrl);
UserCtrl.$inject = ['UserService', '$log'];
function UserCtrl(UserService, $log) {
var vm = this; // vm = viewModel
vm.users = [];
vm.loadUsers = loadUsers;
activate();
function activate() {
loadUsers();
}
function loadUsers() {
return UserService.getAll()
.then(function(users) {
vm.users = users;
return vm.users;
});
}
}
// ✓ CORRECT - service for business logic
angular.module('myApp')
.factory('UserService', UserService);
UserService.$inject = ['$http', '$log'];
function UserService($http, $log) {
var service = {
getAll: getAll,
getById: getById,
create: create
};
return service;
function getAll() {
return $http.get('/api/users')
.then(function(response) {
return response.data;
});
}
function getById(id) {
return $http.get('/api/users/' + id)
.then(function(response) {
return response.data;
});
}
function create(user) {
return $http.post('/api/users', user);
}
}
// ✓ CORRECT - directive for DOM manipulation
angular.module('myApp')
.directive('userCard', userCard);
function userCard() {
return {
restrict: 'E',
scope: {
user: '=',
onSelect: '&'
},
templateUrl: 'user-card.html',
controller: 'UserCardCtrl',
controllerAs: 'vm',
bindToController: true
};
}
// ✓ CORRECT - explicit $inject annotation (minification-safe)
MyCtrl.$inject = ['$scope', '$http', 'UserService'];
function MyCtrl($scope, $http, UserService) {
// ...
}
// ✗ INCORRECT - inline array (verbose) or no annotation (breaks minification)
angular.module('myApp').controller('MyCtrl', ['$scope', function($scope) {}]);
// ✓ CORRECT - one module definition per file
// app.module.js
angular.module('myApp', ['ngRoute', 'myApp.users', 'myApp.auth']);
// users/users.module.js
angular.module('myApp.users', []);
// ✗ INCORRECT - defining everything in one file
angular.module('myApp', []).controller(...).service(...).directive(...);
<!-- ✓ CORRECT - controllerAs in template -->
<div ng-controller="UserCtrl as vm">
<h1>{{ vm.title }}</h1>
<ul>
<li ng-repeat="user in vm.users track by user.id">
{{ user.name }}
</li>
</ul>
<button ng-click="vm.loadUsers()">Reload</button>
</div>
| Mistake | Correct Approach |
|---|---|
| Business logic in controllers | Move to services/factories |
Using $scope directly | Use controllerAs with vm alias |
| Implicit DI (breaks minification) | Use $inject or array annotation |
| DOM manipulation in controllers | Use directives |
| Everything in one module | Organize by feature into sub-modules |
Using $rootScope for shared state | Use a service instead |
| Watchers for every change | Use one-time binding :: where possible |
npx skills add testdino-hq/google-styleguides-skills/angularjs
See angularjs.md for complete details, examples, and edge cases.
Complete collection of Google's official style guides for 17 languages. Includes TypeScript, JavaScript, Python, Java, Go, C++, C#, Swift, Objective-C, HTML/CSS, AngularJS, Shell, R, Common Lisp, Vim Script, JSON, and Markdown. Production-ready coding standards used across Google's engineering organization, formatted for AI agent consumption.
Google's official Common Lisp style guide. Covers naming conventions, formatting, macros, packages, documentation strings, and Lisp best practices.
Google's official C++ style guide. Covers headers, naming conventions, formatting, classes, memory management, RAII, smart pointers, and modern C++ features.
Google's official C# style guide. Covers naming conventions, formatting, LINQ, async/await, XML documentation, and .NET best practices.
Google's official Go style guide. Covers gofmt formatting, naming conventions, error handling, interfaces, concurrency patterns, and package organization. Enforces idiomatic Go code with short variable names and explicit error checks.
Google's official HTML/CSS style guide. Covers formatting, naming, semantics, accessibility, and best practices for web markup and styling.