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!

raoen
  • 37
  • 1
  • 1
  • 11
  • 1
    please provide the source code of setA, setB and setC function. – mtamma May 17 '16 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.. – raoen May 17 '16 at 10:55
  • how do you define your route? is one of your route using menu.html? – mtamma May 17 '16 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 – raoen May 17 '16 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.... – raoen May 17 '16 at 16:09
  • FYI, it's generally a bad practice to use services as data repositories. There are some services that act as a _bus between_ you and a repository, which is fine, but they shouldn't really be the actual data destination. – Harris May 17 '16 at 16:55
  • @Harris : sorry.. but how can I share data between two modules of angular js?. I also tried ngStorage...but it doesn't work... it just create an array of empty object...I don't know... – raoen May 17 '16 at 17:21

2 Answers2

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.

Ashok Mandal
  • 278
  • 2
  • 13
  • Hi, I need to define a link in another page with another Angular js module, and they just need to share array of object... http://stackoverflow.com/questions/26594140/how-to-share-data-between-two-modules-in-angularjs.... is just in another page.html... – raoen May 17 '16 at 16:12
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");

Sid
  • 801
  • 8
  • 19
  • I'm following this article...http://stackoverflow.com/questions/26594140/how-to-share-data-between-two-modules-in-angularjs... it works.... but when I put href ...the value are undefined... – raoen May 17 '16 at 15:32