How to redirect page in Angular?

8

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

Follow 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");
    });
};

});
    
asked by anonymous 11.01.2016 / 18:44

2 answers

9

State usage should not be used to redirect to a URL. As the name says, $ state.go - > go to the state.

That is, 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 your url.

It's important to note that you're not 'Redirecting page', you're just changing its state, ie changing the content assigned to the view.

    
11.01.2016 / 19:02
4

I would use $ location to redirect.

app.controller('myController', function($location) {
  // ... //
  $location.path('/path');
});
    
11.01.2016 / 18:49