mindparse
mindparse

Reputation: 7255

Encountering strictdi error for a controller where $inject is being used

I have enabled strict-di on my application as I am trying to prepare my source code for minification and am now working through resolving strictdi errors being thrown.

I have the following controller thats throwing a strictdi error, but I am correctly annotating using $inject (I am using the John Papa style guide too) and I cannot figure out where I am going wrong:

(function () {
  'use strict';

      angular
        .module('app.products')
        .controller('ProductsPageController', Controller);

      Controller.$inject = ['MyToolbarService'];

      function Controller(MyToolbarService) {
        MyToolbarService.getToolbar('main').then(function (toolbar) {
          toolbar.setTitle('GENERAL_TERMS.PRODUCTS');
        });
      }
    })();

Error: [$injector:strictdi] ProductsPageController is not using explicit annotation and cannot be invoked in strict mode

I have another controller (below) that works in exactly the same manner, and when the view loads that this is bound, no error is thrown and my toolbar service is setting its title:

(function () {
  'use strict';

  angular
    .module('app.home')
    .controller('HomePageController', Controller);

  Controller.$inject = ['MyToolbarService'];

  function Controller(MyToolbarService) {
    MyToolbarService.getToolbar('main').then(function (toolbar) {
      toolbar.setTitle('GENERAL_TERMS.WELCOME_MESSAGE');
    });
  }
})();

Banging my head against the wall now! Anyone got any suggestions?

Thanks

Upvotes: 0

Views: 34

Answers (1)

Sean Larkin
Sean Larkin

Reputation: 6430

I typically don't write my DI in this fashion, most of the time I pass the array as the second argument to the .controller() function.

As to why you are having issues I am not sure. Could it be indentation? (crazy as it seems).

If you wanted to rule anything out I suppose you could try and write it:

(function () {
  'use strict';

  angular
    .module('app.home')
    .controller('HomePageController', ['MyToolbarService', Controller]);

  function Controller(MyToolbarService) {
    MyToolbarService.getToolbar('main').then(function (toolbar) {
      toolbar.setTitle('GENERAL_TERMS.WELCOME_MESSAGE');
    });
  }

})();

Upvotes: 1

Related Questions