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 minimum record in the api and returns 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 doesn’t come with the data that was assigned to it after saving the user, It comes empty the way I declared it 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!!
Girl, but if you set a
service
to do this, it should not return an object, rather than a$rootScope
?– Wallace Maxters
Detail.... This one
http.post
is an angular lib or is using some other Javascript framework? Make a test by leaving a$watch
registered at$rootScope
with the valuenomeMinimo
. Type$rootScope.$watch('nomeMinimo', (v) => console.log(v))
– Wallace Maxters
Wallace I will return an object, but I need to pass the Minimum data to the customer to finish their registration on another page. I just need to keep the models' names.
– Gabriela Mendonça
Ah and I’m using Angularjs
– Gabriela Mendonça
Gabriela, but if you put the value directly on
maisservico
doesn’t work? Usually I do this at the angle and work smoothly. Example:maisservico.nomeMinimo = data.name
– Wallace Maxters
If you can access it, you can log in to chat to better explain the problem. I think it would be nice to put the full source of
maisservico
(of course, if it doesn’t display some sensitive information)– Wallace Maxters
I asked you about the method you use to requisition
$http
because if it is an external library (read "that is not adapted to the angular")talvez seja necessário executar um
$Scope. $apply()depois do
$window.Location`– Wallace Maxters
Oh got it, I’m trying to use only the most saude.nameMinimo, I even have an isLogin, however it is in the same controller, already this minimum name I want to move from a service to a controller
– Gabriela Mendonça
Let’s go continue this discussion in chat.
– Wallace Maxters
Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).
– Sorack