Communication between two Angular Controllers

2

I have an object that I would like to share with other Controllers , I thought in two ways, the first would be to use $rootScope and in the other to use a Service . I opted for the second way and created the following service:

AuditoriaAPP.factory('Service', function() {
 var Service = {
    solicitacoes: {}
  };
 return Service;
});

On the 1st Controller, which is where my object I want to share I do this:

Service.solicitacoes = solicitacao;

So far so good, the object is successfully assigned and can retrieve it in the 2nd Controller :

$scope.solicitacoes = Service.solicitacoes;

The problem is that when I refresh the page the object becomes empty. How can I make a communication between these Controllers without the object being empty when updating the page?

    
asked by anonymous 25.02.2016 / 19:21

1 answer

2

I solved the problem with the help of @lbotinelly, as he told me, every access to the service will always initialize the object and an alternative would be to use localStorage .

In 1st Controller send object to locaStorage

$localStorage.solicitacao = solicitacao;

And then I retrieve the object in the other controllers :

var solicitacao = $localStorage.solicitacao

After use I can delete the object in this way: delete $localStorage.solicitacao

OBS: I used the ngStorage library to work with localStorage

    
25.02.2016 / 19:50