Skip to content

Instantly share code, notes, and snippets.

@jackofseattle
Last active August 29, 2015 14:07
Show Gist options
  • Save jackofseattle/7f0ef4fd50f73bfc8878 to your computer and use it in GitHub Desktop.
Save jackofseattle/7f0ef4fd50f73bfc8878 to your computer and use it in GitHub Desktop.
Basic injectable class for AngularJS
/**
* injectable
*
* Provides a constructor to move items from the $inject property to local object members.
*
* inject = ['$q', '$http'] //translates to
*
* this.$q
* this.$http
*/
export class Injectable {
static set inject(...args) {
this.prototype.$inject = this.$inject = args;
}
static get inject() {
return this.$inject || [];
}
constructor(...args) {
this.inject.forEach((dep, idx) => {
if (args.length < idx - 1) {
throw new Error(`could not read ${dep} - no more arguments passed`);
}
this[dep] = args[idx];
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment