Skip to content

Instantly share code, notes, and snippets.

@shuhei
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shuhei/09d784ba3b3443b36285 to your computer and use it in GitHub Desktop.
Save shuhei/09d784ba3b3443b36285 to your computer and use it in GitHub Desktop.
AngularJS: Bind injected arguments to instance
// @ngInject
class AwesomeController {
constructor(a, b, c, d, e, f, g, h) {
bindArguments(this, arguments);
}
foo() {
this.a.doSomething();
this.b.doAnotherThing();
// ...
}
}
// AwesomeController.$inject = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
// @ngInject
class AwesomeController {
constructor(a, b, c, d, e, f, g, h) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
this.g = g;
this.h = h;
}
foo() {
this.a.doSomething();
this.b.doAnotherThing();
// ...
}
}
// AwesomeController.$inject = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
function bindArguments(instance, args) {
var ctor = instance.constructor;
if (!angular.isArray(ctor.$inject)) {
return;
}
ctor.$inject.forEach(function(argName, i) {
instance[argName] = args[i];
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment