Reputation: 447
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 ??
Upvotes: 0
Views: 579
Reputation: 20139
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
}
});
Upvotes: 1