Tushar
Tushar

Reputation: 3052

How to annotate dependencies in an inline controller while configuring app routes?

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 ?

Upvotes: 0

Views: 283

Answers (2)

dchhetri
dchhetri

Reputation: 7136

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.

Upvotes: 1

rrd
rrd

Reputation: 5977

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.

Upvotes: 1

Related Questions