0

I'm getting following error if I don't annotate the dependencies for an inline controller function for a route (I'm using strict DI mode and all other codes are annotated so that js-minification doesn't break my code):

https://docs.angularjs.org/error/$injector/strictdi?p0=function(AuthService,%20$state

Here is the logout route code:

app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider', '$urlRouterProvider) {

  $stateProvider.state('logout', {
     url: '/logout',
     controller: function(AuthService, $state) {        
       AuthService.logout();
       $state.go('login');
     }
  }

}]);

Is there any technique to declare inline annotation for the above two dependent services (AuthService, $state) of the inline controller ?

I know the bellow work-around :

.state('logout', {
    url: '/logout',
    controller: LogoutController
});


function LogoutController (AuthService, $state) {        
    AuthService.logout();
    $state.go('login');
}
LogoutController.$inject = ['AuthService', '$state'];

this works but just wanted to checkout if anyone knows any smart short-cut ?

2 Answers 2

1

Try

app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
  $stateProvider.state('logout', {
     url: '/logout',
     controller: ['AuthService', '$state', function(AuthService, $state) {        
       AuthService.logout();
       $state.go('login');
     }]
  }
}]);

Not sure if that will work. Usually we separate our controllers into files for ease of use, rather than writing them inline in the config.route.js file.

Sign up to request clarification or add additional context in comments.

3 Comments

It worked ! Thanks a lot :) Didn't try that as I thought it would be an array syntax instead of function. You are right that controllers are separated out in separate files, but here I just have two lines of logic, so thought not to create overhead of processing another file.
Happy to have helped!
Yeah, it will help many others too for sure. Thanks again for your time.
1

Just to add more details here, this is expected for inline controllers.

See https://github.com/olov/ng-annotate/issues/50.

Either not inline them or add apply controller: /* @ngInject */ function(service1){ ... }.

The /* @ngInject */ tells ngannotate to apply annotation here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.