Generates Angular injection annotations for Controllers, Factories and Services
Generates Angular declarations for Factories, Services and Controllers
This plugin requires Grunt
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
npm install grunt-angular-annotator --save-dev
Annotates your Service, Factory and Controller objects for Angular dependency injection. Eliminates the need to maintain redundant boilerplate code for each object.
You write this:
module = angularmodule 'MyModule'=# service implementation=# factory implementation for MyOtherService=# controller implementation
moduleservice'MyService' '$http''$q';modulefactory'MyOtherService' '$resource''MyService' MyOtherServiceFactory;modulecontroller'MyController' '$location''MyOtherService' MyController;
We are developing our user interface using AngularJS and CoffeeScript. All of our source code files have the following structure:
module = angularmodule 'SomeModule''Module Dependency A''Module Dependency B'# factory method for SomeService=:return ...:return ...moduleservice 'SomeService''$http''$q''AnotherOneOfOurServices'SomeService
The same basic pattern applies for Factory and Controller objects.
Needless to say, the need to manually keep the Angular object declaration in sync with the factory method parameters gets annoying very quickly. If you modify the factory method arguments (add, remove or rearrange parameters) but fail to update the Angular object declaration to match, Angular does not complain. Instead you wind up with shifted and/or undefined parameter values at runtime, accompanied by some potentially head-scratching errors.
In your project's Gruntfile, add a section named
angular to the data object passed into
gruntinitConfigcoffee:compile:files:expand: truecwd: 'src'src: '**/*.coffee'dest: 'tmp'ext: '.js'angular:postcompile:src: 'tmp/**/*.js'
As long as you follow the conventions outlined below, you can then remove the manual
angular.controller declarations from your source code.
- Each file contains source code for a single module
- The variable
modulerefers to the angular module for the current file
- All of your controller objects end in the suffix
- All of your directive methods end in the suffix
- All of your service objects end in the suffix
- All of your factory methods end in the suffix
- The service obtained from a factory object has the same name as the factory object, without the
These seem like decent conventions to me, but I'm open to making it more flexible.
In addition to injectable objects, the annotator also handles methods invoked via
angular.config. Simple replace this:
modulerun'$resource''MyService'// do some application initialization here;
// do some application initialization here;
Any method that ends in
Config will get annotated appropriately. ('Init' would be a nicer suffix than 'Run', but for the sake of consistency with angular I won't rock the boat).
None at this time
Gruntfile.js in the project source code for a configuration example.
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.
0.0.1 Initial release
0.0.2 Fix handling of methods with no injected dependencies; added support for Directives, Controllers, Config and Run methods; renamed task from angularAnnotate to just angular