Skip to content

Instantly share code, notes, and snippets.

@jamsesso
Last active March 27, 2016 16:56
Show Gist options
  • Save jamsesso/dd0ed60de1e21997e794 to your computer and use it in GitHub Desktop.
Save jamsesso/dd0ed60de1e21997e794 to your computer and use it in GitHub Desktop.
ES6 Directives in Angular - Decorators
function Service(name) {
return target => {
target.__service = { name };
return target;
}
}
function Controller(name, stateOpts) {
return target => {
target.__controller = { name, stateOpts }; // stateOpts get passed to $stateProvider during registration.
return target;
}
}
function Directive(name) {
return target => {
target.__directive = { name };
return target;
}
}
function Inject(...dependencies) {
return target => {
target.$inject = dependencies;
return target;
}
}
// Example usage for services:
class MyService {
constructor($http, $q) {
// ...
}
}
export default Inject('$http', '$q')(Service('myService')(MyService));
// Or, if you're adventurous:
@Service('myService')
@Inject('$http', '$q')
export default class MyService {
constructor($http, $q) {
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment