Mitigates angular module boilerplate with ES2015 modules based on naming conventions:
import template from "./modal.html";
import UserService from "./UserService";
import AnotherService from "./AnotherService";
function ModalDirective() {
return {
template,
scope: {},
link() {
...
}
};
};
... becomes ...
import { angular } from "angular";
import UserService from "./UserService";
import AnotherService from "./AnotherService";
function ModalDirective(UserService, AnotherService) {
return {
template,
scope: {},
link() {
}
};
}
export default angular.module("app")
.directive("Modal", ["UserService", "AnotherService", ModalDirective])
- Parse the imports
- If an import name starts with a capital letter and ends on
Controller
orService
do the following steps:- Store the import name in a record
- Add
import { angular } from "angular"
to the module's root scope - Add
export default angular.module("app")
to the module's root scope - Parse function names in the module's root scope
- If a function starts with a capital letter and ends on
Directive
,Controller
orService
do the following steps:- Add
angular.module("app")[postfix](name, [...importNames, fn])"
withname
being the function's name withoutDirective
if present...importNames
being the list of imports from the import recordpostfix
being the end of the function name.toLowerCase()
(e.g."directive"
)fn
being the function
- Add
...importNames
as function arguments
- Add
- Less boilerplate
- No dependency injection visible in the code
- Function names of directives, services and controllers must be unique across the
"app"
module - Works best if there is only one directive/service/controller per module