1

I need to save data in a service factory after a ajax call. It all works without location.href. But it doesn't work when I put the line code $window.location.href. This is the code:

 scope.res = function(id){
            factoryService.getLista(id,"en-US","") 
            .then (function(response){commonVarService.setA(response.A);
                                      commonVarService.setB(response.B);
                                      commonVarService.setC(response.C);
                                      $window.location.href="./menu/menu.html";
                                     })
            .catch(function(err)  {console.log(err)});
        };

Where, factoryService allow to make an ajax call, wherease commonVarService allow to memorize data in variables of service. If there isn't the $window.location.href="" it all works, but when there is the $window.location.href, the application redirect in the new page without memorize the variable. where is it my Error? Is there some good solution? Thanks in advance.

the commonVarService code is this:

angular.module('common_variable',[]) .service('commonVarService',function(){

        /*a*/
        this.getA = function(){
            return this._a;
        }

        this.setA = function(a){
            return this._a=a;
        }

        /*b*/
        this.getB = function(){
            return this._b;
        }

        this.setB = function(b){
            return this._b = b;
        }

        /*c*/
        this.getC = function(){
            return this._c;
        }

        this.setC = function(c){
            return this._c = c;
        }

});

I also tried to put simply only the string as parameter:

commonVarService.setA("A"); commonVarService.setB("B"); commonVarService.setC("C")

but when I'm in the new page.html of href link, the variable A , B, C are undefined...I don't know..

Thanks in adavance!

7
  • 1
    please provide the source code of setA, setB and setC function. Commented May 17, 2016 at 10:43
  • Hi, I put the code of set. It works, but when i put the $window.location.href="", it doesn't work. I don't understand why.. Commented May 17, 2016 at 10:55
  • how do you define your route? is one of your route using menu.html? Commented May 17, 2016 at 14:07
  • no, I don't define a rout... I'm just following this: stackoverflow.com/questions/26594140/…... and it works.... but with href...dosen't save the data...I don't want to define a routing, is just a link with arrays of object to share between two different module Commented May 17, 2016 at 15:42
  • I think, the line code $window.location.href="./menu/menu.html" is called before the result of ajax...but I don't know how to resolve it.... Commented May 17, 2016 at 16:09

2 Answers 2

1

So, I am explaining the problem here, When you are using$window.location.href = 'something' . It does a full page refresh, so it cannot store the variable,reinstatiate a new angular instance. i would suggest to use , angular-route or angular-ui-router , where you can change the route by using this , $location.path('/profile') or $location.path('/mypage') if you are building a SPA(Single page application) ,which will retain the state of your page.

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

1 Comment

Hi, I need to define a link in another page with another Angular js module, and they just need to share array of object... stackoverflow.com/questions/26594140/….... is just in another page.html...
0

Issue is with scope of variables when you are changing current page location it will load all js files again so it will also declare new variables


"var service={}; var _a = null; var _b = null; var _c = null;"



Define above variables in "localstorage"

i.e. to Set Value : localStorage.setItem("_a ", a);
to Get Value : localStorage.getItem("_a");

1 Comment

I'm following this article...stackoverflow.com/questions/26594140/…... it works.... but when I put href ...the value are undefined...

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.