I am working on an AngularJS application (1.4.10) where I need to share data between two controllers.
To achieve this, I created a factory:
.factory('CardsForService', function($http, URL){
var service = {
"block_id": '',
"service_id": ''
};
service.save_data = function(block_id, service_id){
service.block_id = block_id;
service.service_id = service_id;
};
service.get_data = function(){
return service;
};
return service;
})
I set the data in the first controller:
$scope.open = function(id, type){
console.log(id +" "+type);
CardsForService.save_data(id, type);
...
And when attempting to retrieve the data in another controller, it seems to return empty:
$scope.$on('$routeChangeSuccess', function() {
if (algo_to_used == "service"){
var data = CardsForService.get_data();
console.log(data);
} else {
}
});
The output of console.log shows:
Object {block_id: "", service_id: ""}
However, when I use the get_data() function in the same controller as save_data(), it works fine. What could be causing this issue?