I'm having trouble reading a variable from a service in other controllers

1

I have the following problem:

I have a service where I create a variable that I will manipulate and send to another controller.

This is my service

app.service("maissaude", [
  "$rootScope",
   function($rootScope) {
    $rootScope.nomeMinimo = ""; //Variável que vou manipular e ler em outro controller
}]);

In this same service I have a method that saves a minimal record in api and returns me the saved user:

minimumRegistration: function(form, doctor) {
      maissaude.http.post({
        url: "url que vai na api salvar o dado",
        data: maissaude.signup.modelMinimum,
        callback: {
          success: function(data) {
            var nomeMinimo = data.name;
            $rootScope.nomeMinimo = nomeMinimo;//Atualizo meu nomeMinimo da mesma forma que fiz lá no início do método
            $window.location = "/login/#/cadastro/" + data.id;
          }
        }
      });
}

In another controller I read this $ rootScope.nomeMinimo But it does not come with the data that was assigned to it after saving the user, It comes empty the way I said up there;

app.controller("CadastroController", [
  "$scope",
  "$rootScope",
  "maissaude",
  function($scope, $rootScope, maissaude) {
    console.log("Variável global!!!!!!!!!!!!!!!!!");
    console.log($rootScope.nomeMinimo);
  }
]);

Someone can tell me what I have to do to be able to read this console, the data I manipulated in the other service. PLease !!

    
asked by anonymous 28.06.2018 / 21:29

1 answer

1

Using $rootScope to store a global variable is not a good practice because your variable will be available everywhere in the application without proper processing. You started with the right idea to use service for this. I'll leave a simple example of how to share variables using two (or more) controllers .

angular
  .module('minhaApp', []);

// Service para compartilhamento dos dados
angular
  .module('minhaApp')
  .factory('principalService', principalService);

principalService.$inject = [];

function principalService() {
  var service = {
    informacoes: {
      nomeMinimo: ''
    }
  };

  return service;
};

// Controller Primário
angular
  .module('minhaApp')
  .controller('ControllerPrimario', ControllerPrimario);

ControllerPrimario.$inject = ['principalService'];

function ControllerPrimario(principalService) {
  var vm = this;
  vm.informacoes = principalService.informacoes;
}

// Controller Secundário
angular
  .module('minhaApp')
  .controller('ControllerSecundario', ControllerSecundario);

ControllerSecundario.$inject = ['principalService'];

function ControllerSecundario(principalService) {
  var vm = this;
  vm.informacoes = principalService.informacoes;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><divng-app="minhaApp" style="display:inline-flex">
  <div ng-controller="ControllerPrimario as vm" style="border:solid black">
    <input type="text" ng-model="vm.informacoes.nomeMinimo" />
  </div>
  
  <div ng-controller="ControllerSecundario as vm" style="border:solid black">
    <input type="text" ng-model="vm.informacoes.nomeMinimo" />
  </div>
</div>

You may notice in the example above that I just created a informacoes object with a nomeMinimo attribute in service . Within the controllers I made the assignment of this attribute to a variable that can be manipulated within both controllers mirroring this behavior for all contexts.

    
23.07.2018 / 14:53