How to uninject a dependency previously included using $inject in AngularJS

225 views Asked by At

I've been reading the official docs https://docs.angularjs.org/api/auto/service/$injector but failed to find how to de-inject a dependency.

My scenario:

I have a service that authenticates users, and a number of services that only make sense for authenticated users.

I'd like to handle a successful login-logout programatically, and inject-uninject the other services depending on whether the user is logged in.

Does anyone know if it's possible to dynamically de-inject a dependency in an AngularJS app?

1

There are 1 answers

0
yangli-io On
angular.module('app',[]).controller('appCtrl', ['userService', function(userService){
    if (userService.loggedOut){
        userService = undefined;
    }
}])

http://plnkr.co/edit/0R64sqdEfBPUjhKu6tPK?p=preview