Change view by controller (Ng-Click)

1

I'm using ng-route to develop a single page application with angularjs + laravel. I'm having a problem with the swap view with ng-click and controller. I have a button in a view, and I would like it to be clicked by redirecting it to another view, taking an info (id).

This is my angular route file. I want to redirect from the Manage Employees page to the New Contributor page.

angular.module('registrosGerais')
			.config(function($routeProvider) {
		      $routeProvider
		      .when('/', {
		          templateUrl: 'views/index.html',
		          controller: ''
		        })
		        .when('/colaboradores', {
		          templateUrl: 'views/colaboradores/gerenciarcolaboradores.html',
		          controller: 'colaboradoresController'
		        })
		        .when('/colaboradores/novocolaborador', {
		          templateUrl: 'views/colaboradores/novocolaborador.html',
		          controller: 'colaboradoresController'
		        })
		        .otherwise({
		          redirectTo: '/colaboradores'
		        });
		    });

This is the button that must be clicked to redirect the new view.

<button id="btn-add" class="btn btn-primary btn-xs"
        ng-click="toggle('add', 0, '/#/colaboradores/novocolaborador')">Novo Colaborador</button>

This is my controller.

app.controller('colaboradoresController', function($scope, $http, API_URL) {
    
    $http.get(API_URL + "colaboradores")
            .success(function(response) {
                $scope.tbcolaborador = response;
            });

    $scope.toggle = function(modalstate, id, $location) {
        $scope.modalstate = modalstate;

        $location.path('/#/colaboradores/novocolaborador');
}

The exchange of view should be done by taking an information (0 case to add a new one) and the id if it is an existing collaborator. Thanks in advance for your help.

    
asked by anonymous 30.08.2016 / 02:55

1 answer

0

As the controller is discarded in the route change, one way is to prepare your config router to receive the id parameter in the url itself.

.when('/colaboradores/novocolaborador/:id', {
              templateUrl: 'views/colaboradores/novocolaborador.html',
              controller: 'colaboradoresController'
            })

So you can optionally pass the id through the url (concatenating with the variable, for example):

$scope.toggle = function(modalstate, id, $location) {
    $scope.modalstate = modalstate;

    $location.path('/colaboradores/novocolaborador/' + id);

... and retrieve the same when in the other view, via $ routeParams. Do not forget to inject this service into your controller.

var id = $routeParams.id;
    
30.08.2016 / 18:25