Created
February 7, 2018 11:38
-
-
Save Grohden/865ff9230b13f5bdb122b990a40800ee to your computer and use it in GitHub Desktop.
Get rid of AngularJS dependency injection using webpack
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
//An memoize fn would be good to use here. | |
const getService = (serviceName) => angular.element(document.body).injector().get(serviceName); | |
export default new Proxy({}, { | |
get (target, prop) { | |
return getService(prop); | |
} | |
}); | |
/* | |
* Or if you dont want to use an polyfill to proxy you can define every service with: | |
* | |
* export default { | |
* get $http(){ | |
* return getService('$http'); | |
* } | |
* // other gets here. | |
* } | |
* | |
*/ |
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
import AngularServices from 'AngularServices.js'; | |
// There are some limitations: | |
// 1 - you can't import a service at top, angular has to init all services before so you have to do it at functions. | |
// 2 - you still need the run and config phases, but services and factories can be removed in flavor of just files | |
// exporting what needs to be used. | |
function testsJS(){ | |
const {$http} = AngularServices; | |
$http.get() //etc | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment