olov / ng-annotate

Add, remove and rebuild AngularJS dependency injection annotations
MIT License
2.03k stars 152 forks source link

ng-annotate not working if exporting a module #224

Closed davidreher closed 8 years ago

davidreher commented 8 years ago

I have some code which looks like this:

export class PlainTextCtrl {
  /* @ngInject */
  constructor($scope) {
    this.containerCtrl = $scope.containerCtrl;
  }
}

after running it through babel, I am getting the following:

var PlainTextCtrl =
  /* @ngInject */
  exports.PlainTextCtrl = function PlainTextCtrl($scope) {
    _classCallCheck(this, PlainTextCtrl);
    this.containerCtrl = $scope.containerCtrl;
  };
}

When I run this through ng-annotate nothing happens. But it should add the following:

PlainTextCtrl.$inject = ['$scope'];

or

exports.PlainTextCtrl.$inject = ['$scope'];

how to get around this issue?

I am using ng-annotate 1.2.0

davidreher commented 8 years ago

What works is using 'ngInject' in the constructor, but it would be great to have the other one working as well ;)

olov commented 8 years ago

Using /*ngInject*/ with transpilers is brittle because they tend to rearrange things. "ngInject" is the way to go!

nicbou commented 8 years ago

Here is an example, if you are parsing the thread and wondering if it's what you are looking for:

Before

export default class ItineraryItemService {

  constructor/* @ngInject*/($rootScope, Config, ItineraryItem) {
    ...
  }
...

After

export default class ItineraryItemService {

  constructor($rootScope, Config, ItineraryItem) {
    'ngInject';
    ...
  }
...

Thank you for the help, @olov !