0

I need to use a scope , that is in a controller, in another controller . So I've used a factory :

app.factory('myservice',function(){
    var mydata=[];
    function set(data){
        mydata=data;
    }

    function get(){
        return mydata;
    }

    return {
        set:set,
        get:get
    }
});

Then In the controller that contains the scope I need I set the data :

myservice.set($scope.value)

In the other controller where I need the scope I get it from the factory :

$scope.value= myservice.get()

This seems to work fine . My problem is that when I refresh the page where I'm using the second controller $scope.value becomes undefined .

How to fix this ??

1
  • on page refresh angular context will gone away..there will not be any value available after page refresh Commented May 7, 2015 at 19:38

1 Answer 1

1

Data in services does not persist through page refreshes. There is no way around this.

However, you can store the data in cookies or localstorage, then reload that data on page load. Here is some pseudocode:

//Don't forget to add a dependency on ngCookies at the module level!
app.factory('myservice',function($cookies){
    var mydata=[];
    function set(data){
        $cookies.putObject("myData", data);
    }

    function get(){
        return $cookies.getObject("myData");
    }

    return {
        set:set,
        get:get
    }
});
Sign up to request clarification or add additional context in comments.

Comments

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.