Skip to content

Instantly share code, notes, and snippets.

@LordZardeck
Last active August 29, 2015 14:24
Show Gist options
  • Save LordZardeck/47e95b1d0d3370fa7647 to your computer and use it in GitHub Desktop.
Save LordZardeck/47e95b1d0d3370fa7647 to your computer and use it in GitHub Desktop.
AngularJS TypeScript decorators
function DecorateAngularObject(module: string, target: Object, type: string, className?: string): void {
switch (type) {
case "config":
case "run":
angular.module(module)[type](target);
break;
default:
angular.module(module)[type](className, target);
break;
}
}
function Controller(module: string, className: string) {
return (target: Function) => DecorateAngularObject(module, target, "controller", className);
}
function Directive(module: string, className: string) {
return (target: Function) => DecorateAngularObject(module, target, "directive", className);
}
function Service(module: string, className: string) {
return (target: Function) => DecorateAngularObject(module, target, "service", className);
}
function Factory(module: string, className: string) {
return (target: Function) => DecorateAngularObject(module, target, "factory", className);
}
function Run (module: string) {
return (target: Function) => DecorateAngularObject(module, target, "run");
}
function Config (module: string) {
return (target: Function) => DecorateAngularObject(module, target, "config");
}
function Inject (...args: string[]) {
return (target: any) => {
(<Function>target).$inject = args;
return target;
};
}
@Controller("app.store", "StoreItemDetailController")
@Inject("$scope", "$stateParams")
class StoreItemDetailController {
constructor($scope, $stateParams) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment