Created
July 10, 2014 20:12
-
-
Save callmehiphop/5ddcc68cebc411c66645 to your computer and use it in GitHub Desktop.
A really weird way to inject dependencies with AngularJS
This file contains 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
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)); | |
})); |
This file contains 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
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