How to redirect page in Angular?

Asked

Viewed 7,334 times

8

After finishing the data update, I want the system to redirect the user to another page. I am using ui-router and I was told to use $state.go. But how do I use this command?

Follows my code:

app.controller("AtualizarUsuarioController", function ($scope, $window, $http, $state) {

$scope.usuario = {
    'id': $window.localStorage.getItem('idUsuarios'),
    'nome': $window.localStorage.getItem('nome'),
    'email': $window.localStorage.getItem('email')
}

//$location.path('/atualizarUsuario' + $scope.usuario.id);
$scope.atualizarUsuario = function (usuario) {
    $http.post("admin/php/atualizarUsuario.php", usuario).then(function (data){

        $state.go("/usuario");
    });
};

});

2 answers

9


The use of state should not be used to redirect to a URL. As the name itself says, $state.go -> go to the state.

I mean, instead of:

$state.go("/usuario");

Use:

$state.go("usuario"); //exemplo do .state abaixo

.state("usuario", {
    url:"/Usuario",
    controller: "UsuarioCtrl",
    templateUrl: "meu/caminho/usuario.html"
})

Where usuario is the state NAME, not its url.


Important to note that you are not 'Redirecting the page', you are just changing the status of the page (state), that is, changing the content assigned to the view.

  • String Absolute State Name or Relative State Path According to the example, it refers to the relative state, ie if it is a Child state, or return to the Parent state, etc. But always with reference in the name of the .state or the equivalent character, as is the use of ^ to go to state dad.

  • And what do you put in that "..."? . state('user', ...)?

  • 1

    It’s just the setting when you create the state, where you assign the controller, template, etc. It was just an example.

  • Ahhh yes, right.

4

I would use the $Location to redirect.

app.controller('myController', function($location) {
  // ... //
  $location.path('/path');
});
  • 1

    Yeah, I tried, but they said I had to use $state 'cause I’m using it on my routes.

  • Only, too, it’s not working.

  • Sorry, I did another test and it worked.... But how to use $state.go?

Browser other questions tagged

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