How to redirect view in a controller with Angularjs

Asked

Viewed 960 times

1

I have a department consultation page and I have the sign-up button on it. This page is a "view" that has been set to route and is called within my index.html.

Gosaria to know how it is possible to click on the sign up button, change the view I am to the registration view I will use.

I have to use some specific angular service ?

these are my routes:

$routeProvider
    .when("/cadastro/produtos", {
        templateUrl: 'view/consultaDeProdutos.html',
        controller: 'productRegisterController as vm'
    })

    .when("/cadastro/categorias", {
        templateUrl: 'view/consultaDeCategorias.html',
        controller: 'categoryRegisterController as vm'
    })

    .when("/cadastro/departamentos", {
        templateUrl: 'view/consultaDeDepartamentos.html',
        controller: 'departmentRegisterController as vm'
    })

    .when("/cadastro/departamentos/salvar", {
        templateUrl: 'view/cadastroDeDepartamentos.html',
        controller: 'departmentRegisterController as vm'
    })

I want the route /registration/departments to go to /registration/departments/save

through a button

  • if you have any code?

  • Yeah, I already added it to the question

  • Why do you have vm alias for all controllers? this is not wrong?

  • What do you mean for all controllers? I use an alias not to use $Scope

  • Henry puts all the code

1 answer

0


You need to use the service $location to change routes/views.

app.controller('departmentRegisterController', ['$location', '$scope', function ($location,$scope) {

    var vm = this;

    vm.Cadastrar = function() {
        $location.path('/cadastro/departamentos/salvar');
    }

}]);

From there on the button you can use so:

<button ng-click="vm.Cadastrar()">Cadastrar</button>

In the registration screen controller (in your case it’s the same controller), you can create another method to go back to the department list:

vm.ExibirListaDeDepartamentos = function() {
    $location.path('/cadastro/departamentos');
}

And create a button Cancel or Come back like this:

<button ng-click="vm.ExibirListaDeDepartamentos()">Cancelar</button>
  • I had tried this way too, but it did not work... he understands that I am passing a route nonexistent and falls in the "othrewise" redirecting to my index. You know why that?

  • @Henry that strange. Is using exactly "/cadastro/departamentos/salvar"? You can include your controller code where you use the $location? At least for the view "/cadastro/departamentos" you can go?

  • It worked Alisson, I discovered a syntax error in my index.... then this procedure worked perfectly. Thank you! You think I’d better change my routes and use ui-router state than ngRoute ?

  • @Henrique the ui-router has everything ngRoute has and a few other things, the most attractive thing is that the ui-router allows you to use nested views, while in ngRoute you can only use one view at a time. For simpler apps and if you don’t have time to learn ui-router, use ngRoute anyway. But if you already know or have time, I suggest ui-router.

  • Dude this $Location uses some kind of cache ? He’s redirecting to the view I chose, but any changes I make in html do not reflect in the view.... And I’m saving everything right...

  • @Henrique actually stores static cached files such as css, js and html by default. Try pressing CTRL + F5 and see if it works.

Show 1 more comment

Browser other questions tagged

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