Skip to content

Instantly share code, notes, and snippets.

@rixlabs
Last active December 7, 2015 15:14
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 rixlabs/b159d6296f868e8721f6 to your computer and use it in GitHub Desktop.
Save rixlabs/b159d6296f868e8721f6 to your computer and use it in GitHub Desktop.
AngularJS $invoker trick for injecting arguments
//Function that has to receive parameters
//Can be whatever function the injection is like in normal AngularJS CDI so you can inject service or locals
function wrapped(thirdLoc,firstLoc){
console.log('Enter Wrapped');
$timeout(function(){
console.log('Empty inside message');
console.info('Its injected??? ->',thirdLoc);
console.info('Its injected??? ->',firstLoc);
}, 2000);
}
//Injection -> in this case 2 locals
wrapped.$inject=['thirdLoc','firstLoc'];
//Wrapper function that define the object for injection and use invoke for passing the injectable locals
function wrapper(toWrap) {
//locals object
var localsObj = {
firstLoc: 'Ciao',
secondLoc: 42,
thirdLoc: {message: 'Poldo'}
};
//The injector provider inject the locals into the called function
$injector.invoke(toWrap, this, localsObj);
}
wrapper(wrapped);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment