Last active
December 7, 2015 15:14
-
-
Save rixlabs/b159d6296f868e8721f6 to your computer and use it in GitHub Desktop.
AngularJS $invoker trick for injecting arguments
This file contains hidden or 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 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