Search code examples
angularjsecmascript-6angularjs-service

Injecting modules into service AngularJS ES6


I'm new to ES6 syntax of within Angular. I'm trying to create a service and use $http and LocalStorageModule

When I try to inject the two modules into my service I get errors about my syntax not being in strict mode. What am I doing wrong?

services/auth.js

export default class {

  constructor($http, localStorageService) {
    this.$http = $http;
    this.localStorageService = localStorageService;
  }
}

services/index.js

import angular from 'angular';

import auth from './auth';

export default angular
  .module( 'app.services', [] )
  .service( 'Authentication', auth )
  .name;

Using Angular 1.4.5


Solution

  • I was able to solve it by using 'ngInject'; in the constructor function.

    I believe it wasn't working because when 'ngInject' lets Babel transpiler know how to handle this injection

    export default class {
    
      constructor($http, localStorageService) {
        'ngInject';
        this.$http = $http;
        this.localStorageService = localStorageService;
      }
    }