Last active
March 27, 2016 16:56
-
-
Save jamsesso/dd0ed60de1e21997e794 to your computer and use it in GitHub Desktop.
ES6 Directives in Angular - Decorators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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