D
D
Dmitry Arushanov2014-06-10 19:46:14
Angular
Dmitry Arushanov, 2014-06-10 19:46:14

Working with NgStorage or equivalent from Angular module .factory or .config

Good day to all. Such is the question:
There is the following code, which, let's say, makes an add-on to Resctangular through factories.
The essence I need: after each request through Restangular, check the status of the code and perform actions with data in ngStorage

angular.module('app').factory('ApiV1Restangular', ['Restangular', function(Restangular) {

  return Restangular.withConfig(function(RestangularConfigurer) {

    RestangularConfigurer
        .setBaseUrl('/api/v1/')
        .setFullResponse(true)
       .addResponseInterceptor(function(operation, what, url, deferred, response){
            console.log(response.status);

            if(response.status == 401){
                window.location = '#/login'
            }
        });

  });

}]);

Actually the question itself, in a piece of code that will be executed in the addResponseInterceptor parameter
I need to add\change\remove a variable from $localStorage from NgStorage . But when connecting NgStorage - I get an error After reading
Error: [$injector:unpr] .... и т д
in Google I realized that NgStorage cannot be used in .factory.
Are there options for how this can be implemented?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
maxaon, 2014-06-10
@maxaon

What did you read and understand that $localStorage cannot be used in a factory?

D
Dmitry Arushanov, 2014-06-10
@daruwanov

@maxaon
And what, in this case, I have it wrong, with such a record?

angular.module('app').run(['$provide', '$compileProvider', '$locationProvider',
                              '$httpProvider', 'RestangularProvider', 'ngStorage',
    function($provide, $compileProvider, $locationProvider, $httpProvider, RestangularProvider, $localStorage) {

J
JINIK, 2015-06-14
@JINIK

You need to connect ngStorage first:
and then, if necessary, inject it:

App.run(function($localStorage, $sessionStorage) {});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question