Last active
September 26, 2017 16:20
-
-
Save jfairbank/e208c6184174bf849264 to your computer and use it in GitHub Desktop.
ES7 Class Decorators, Properties, and Angular
This file contains hidden or 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
| import angular from 'angular'; | |
| import MyService from './myService'; | |
| import myDirective from './myDirective'; | |
| angular.module('myApp', []) | |
| .service('myService', MyService) | |
| .directive('myDirective', myDirective); |
This file contains hidden or 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
| // Class properties: https://gist.github.com/jeffmo/054df782c05639da2adb | |
| // Decorators: https://github.com/wycats/javascript-decorators | |
| export function inject(...deps) { | |
| return (target, name, descriptor) => { | |
| let injectee = arguments.length === 1 ? target : descriptor.value; | |
| injectee.$inject = deps; | |
| }; | |
| } |
This file contains hidden or 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
| import { inject } from './decorators'; | |
| class MyDirective { | |
| restrict = 'E'; | |
| template = '<div>Hello, my name is {{::name}}</div>'; | |
| scope = { | |
| name: '=' | |
| }; | |
| @inject('$scope', 'myService') | |
| controller($scope, myService) { | |
| // ... | |
| } | |
| } | |
| export default function() { | |
| return new MyDirective(); | |
| } |
This file contains hidden or 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
| import { inject } from './decorators'; | |
| @inject('myConstant', 'myOtherService') | |
| class MyService { | |
| constructor(myConstant, myOtherService) { | |
| this.myConstant = myConstant; | |
| this.myOtherService = myOtherService; | |
| } | |
| doSomething() { | |
| let value = this.myOtherService(this.myConstant); | |
| return value * 2; | |
| } | |
| } | |
| export default MyService; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you can have this as well: