Yeoman generator for AngularJS - lets you quickly set up a project with sensible defaults and best practises.
Install generator-angular
:
npm install -g generator-angular
Make a new directory, and cd
into it:
mkdir my-new-project && cd $_
Run yo angular
, optionally passing an app name:
yo angular [app-name]
Run grunt
for building and grunt serve
for preview
Available generators:
- angular (aka angular:app)
- angular:controller
- angular:directive
- angular:filter
- angular:route
- angular:service
- angular:provider
- angular:factory
- angular:value
- angular:constant
- [angular:decorator] (#decorator)
- angular:view
Note: Generators are to be run from the root directory of your app.
Sets up a new AngularJS app, generating all the boilerplate you need to get started. The app generator also optionally installs Twitter Bootstrap and additional AngularJS modules, such as angular-resource (installed by default).
Example:
yo angular
Generates a controller and view, and configures a route in app/scripts/app.js
connecting them.
Example:
yo angular:route myroute
Produces app/scripts/controllers/myroute.js
:
angular.module('myMod').controller('MyrouteCtrl', function ($scope) {
// ...
});
Produces app/views/myroute.html
:
<p>This is the myroute view</p>
Generates a controller in app/scripts/controllers
.
Example:
yo angular:controller user
Produces app/scripts/controllers/user.js
:
angular.module('myMod').controller('UserCtrl', function ($scope) {
// ...
});
Generates a directive in app/scripts/directives
.
Example:
yo angular:directive myDirective
Produces app/scripts/directives/myDirective.js
:
angular.module('myMod').directive('myDirective', function () {
return {
template: '<div></div>',
restrict: 'E',
link: function postLink(scope, element, attrs) {
element.text('this is the myDirective directive');
}
};
});
Generates a filter in app/scripts/filters
.
Example:
yo angular:filter myFilter
Produces app/scripts/filters/myFilter.js
:
angular.module('myMod').filter('myFilter', function () {
return function (input) {
return 'myFilter filter:' + input;
};
});
Generates an HTML view file in app/views
.
Example:
yo angular:view user
Produces app/views/user.html
:
<p>This is the user view</p>
Generates an AngularJS service.
Example:
yo angular:service myService
Produces app/scripts/services/myService.js
:
angular.module('myMod').service('myService', function () {
// ...
});
You can also do yo angular:factory
, yo angular:provider
, yo angular:value
, and yo angular:constant
for other types of services.
Generates an AngularJS service decorator.
Example:
yo angular:decorator serviceName
Produces app/scripts/decorators/serviceNameDecorator.js
:
angular.module('myMod').config(function ($provide) {
$provide.decorator('serviceName', function ($delegate) {
// ...
return $delegate;
});
});
In general, these options can be applied to any generator, though they only affect generators that produce scripts.
For generators that output scripts, the --coffee
option will output CoffeeScript instead of JavaScript, and --typescript
will out put TypeScript instead of JavaScript.
For example:
yo angular:controller user --coffee
Produces app/scripts/controller/user.coffee
:
angular.module('myMod')
.controller 'UserCtrl', ($scope) ->
For example:
yo angular:controller user --typescript
Produces app/scripts/controller/user.ts
:
/// <reference path="../app.ts" />
'use strict';
module demoApp {
export interface IUserScope extends ng.IScope {
awesomeThings: any[];
}
export class UserCtrl {
constructor (private $scope:IUserScope) {
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'Karma'
];
}
}
}
angular.module('demoApp')
.controller('UserCtrl', demoApp.UserCtrl);
A project can mix TypeScript, CoffeScript, and JavaScript files.
To output JavaScript files, even if CoffeeScript (or TypeScript) files exist (the default is to output CoffeeScript files if the generator finds any in the project), use --coffee=false
and/or --typescript=false
.
Deprecated
Related Issue #452: This option is being removed in future versions of the generator. Initially it was needed as ngMin was not entirely stable. As it has matured, the need to keep separate versions of the script templates has led to extra complexity and maintenance of the generator. By removing these extra burdens, new features and bug fixes should be easier to implement. If you are dependent on this option, please take a look at ngMin and seriously consider implementing it in your own code. It will help reduce the amount of typing you have to do (and look through) as well as make your code cleaner to look at.
By default, generators produce unannotated code. Without annotations, AngularJS's DI system will break when minified. Typically, these annotations that make minification safe are added automatically at build-time, after application files are concatenated, but before they are minified. By providing the --minsafe
option, the code generated will out-of-the-box be ready for minification. The trade-off is between amount of boilerplate, and build process complexity.
yo angular:controller user --minsafe
Produces app/controller/user.js
:
angular.module('myMod').controller('UserCtrl', ['$scope', function ($scope) {
// ...
}]);
Unannotated:
angular.module('myMod').controller('MyCtrl', function ($scope, $http, myService) {
// ...
});
Annotated:
angular.module('myMod').controller('MyCtrl',
['$scope', '$http', 'myService', function ($scope, $http, myService) {
// ...
}]);
The annotations are important because minified code will rename variables, making it impossible for AngularJS to infer module names based solely on function parameters.
The recommended build process uses ngmin
, a tool that automatically adds these annotations. However, if you'd rather not use ngmin
, you have to add these annotations manually yourself. **One thing to note is that ngmin
does not produce minsafe code for things that are not main level elements like controller, services, providers, etc.:
resolve: {
User: function(myService) {
return MyService();
}
}
will need to be manually done like so:
resolve: {
User: ['myService', function(myService) {
return MyService();
}]
}
By default, new scripts are added to the index.html file. However, this may not always be suitable. Some use cases:
- Manually added to the file
- Auto-added by a 3rd party plugin
- Using this generator as a subgenerator
To skip adding them to the index, pass in the skip-add argument:
yo angular:service serviceName --skip-add
The following packages are always installed by the app generator:
- angular
- angular-mocks
- angular-scenario
The following additional modules are available as components on bower, and installable via bower install
:
- angular-cookies
- angular-loader
- angular-resource
- angular-sanitize
All of these can be updated with bower update
as new versions of AngularJS are released.
Yeoman generated projects can be further tweaked according to your needs by modifying project files appropriately.
You can change the app
directory by adding a appPath
property to bower.json
. For instance, if you wanted to easily integrate with Express.js, you could add the following:
{
"name": "yo-test",
"version": "0.0.0",
...
"appPath": "public"
}
This will cause Yeoman-generated client-side files to be placed in public
.
Running grunt test
will run the unit tests with karma.
See the contributing docs
When submitting an issue, please follow the guidelines. Especially important is to make sure Yeoman is up-to-date, and providing the command or commands that cause the issue.
When submitting a PR, make sure that the commit messages match the AngularJS conventions.
When submitting a bugfix, write a test that exposes the bug and fails before applying your fix. Submit the test alongside the fix.
When submitting a new feature, add tests that cover the feature.