Skip to content

Instantly share code, notes, and snippets.

@Grohden
Created February 7, 2018 11:38
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 Grohden/865ff9230b13f5bdb122b990a40800ee to your computer and use it in GitHub Desktop.
Save Grohden/865ff9230b13f5bdb122b990a40800ee to your computer and use it in GitHub Desktop.
Get rid of AngularJS dependency injection using webpack
//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.
* }
*
*/
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