Writing a login service in AngularJS -


i trying refactor angularjs application , introduce login service. controller has methods login(), logout(), getcurrentuser(), make http requests , handle user object. want move these functions service, because need call getcurrentuser() method multiple controllers.

i have written following service:

angular.module('common.login', []).     factory('loginservice', function ($http) {          return {             user: null,             error: null         };     }); 

so @ moment service not doing anything. error:

uncaught error: circular dependency: loginservice <- $http 

if remove $http dependency callback function, service works. have trouble understanding why there circular dependency. loginservice factory injected 2 places: **main module's config callback ** , **login module's controller callback **. config callback configures http interceptor, login controller uses http services.

the circular dependency caused because configure httpinterceptor either send auth token or intercept 401 error depend on login service depend $http service.

your httpinterceptor should not depend of login service. instance if need send token, store in localstorage when login , in httpinterceptor read there instead of loginservice. if need logout user when 401 error juste redirect user /logout instead of calling logout method on loginservice.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -