Skip to content

Instantly share code, notes, and snippets.

@callmehiphop
Created July 10, 2014 20:12
Show Gist options
  • Save callmehiphop/5ddcc68cebc411c66645 to your computer and use it in GitHub Desktop.
Save callmehiphop/5ddcc68cebc411c66645 to your computer and use it in GitHub Desktop.
A really weird way to inject dependencies with AngularJS
angular.module('test', []).controller('Test', inject([
'$scope',
'$http'
], function () {
'use strict';
this.$scope.wat = 'hallo';
this.$http.jsonp('http://reddit.com/.json?jsonp=JSON_CALLBACK').then(function () {
this.$scope.wat = 'woo reddit';
}.bind(this));
}));
function inject (deps, cb) {
function getDeps () {
var args = [].slice.call(arguments);
var ctx = {};
angular.forEach(deps, function (dep, i) {
ctx[dep] = args[i];
});
return cb.call(ctx);
}
return deps.push(getDeps) && deps;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment