Communication between two Angularjs controllers

Asked

Viewed 692 times

2

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

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

In the 1st Controller, which is where my object is that I want to share:

Service.solicitacoes = solicitacao;

So far so good, the object is assigned successfully and I can recover 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 communicate between these Controllers without the object being empty when refreshing the page?

1 answer

2


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

In the first Controller sending the object to the locaStorage

$localStorage.solicitacao = solicitacao;

And then I retrieve the object from the others controllers:

var solicitacao = $localStorage.solicitacao

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

OBS: I used the library ngStorage to work with the localStorage

Browser other questions tagged

You are not signed in. Login or sign up in order to post.