Angular Search Result in another view

Asked

Viewed 652 times

0

I have a search form with 2 selects and 1 Submit:

<select ng-model="marca" ng-options="marca.nome_marca for marca in marcas" ng-change="changeMarca()">
<option value="">Selecione uma Marca</option>
</select>

<select ng-model="modelo" ng-options="modelo.nome_modelo for modelo in modelos|filter:{marca_id:marca.marca_id}" ng-change="changeModelo()">
<option value="">Selecione um Modelo</option>
</select>

<button ng-Click="???">BUSCAR</button>

How can I display the result of this search in another view, more specifically on the route I have already set aside for this: #/busca/:marca_id/:modelo_id

I do not know if I was clear, I already have all the routes, just need to send this to the search controller below:

angular.module("myApp").controller("buscaCtrl", function($scope, $http, $routeParams, estoqueAPI) {
    $scope.loading = true;
    $scope.buscaMarca = $routeParams.marca_id;
    $scope.buscaModelo = $routeParams.modelo_id;

    var carregarEstoque = function () {
        estoqueAPI.getEstoque()
        .success(function (data) {
            $scope.carros = data;
        }).finally(function() { $scope.loading = false; })
    };

    carregarEstoque(); 
});

1 answer

1


Before the excerpt you put in the question the only reason for not working as expected would be to consider the parameters when making the request:

angular.module("myApp").controller("buscaCtrl", function($scope, $http, $routeParams, estoqueAPI) {
    $scope.loading = true;
    $scope.buscaMarca = $routeParams.marca_id;
    $scope.buscaModelo = $routeParams.modelo_id;

    var carregarEstoque = function () {
        estoqueAPI.getEstoque($scope.buscaMarca, $scope.buscaModelo)//<< considerar os parâmetros marca e modelo
        .success(function (data) {
            $scope.carros = data;
        }).finally(function() { $scope.loading = false; })
    };

    carregarEstoque(); 
});

Now... If you want to know what to put in the "ng-click" of the "SEARCH" button, then it would be something like:

<button ng-Click="buscar()">BUSCAR</button>

And the control:

angular.module("myApp").controller("formCtrl", function($scope, $location) {
    
    $scope.buscar = function(){
        $location.path('/busca/' + $scope.marca.marca_id + '/' + $scope.modelo.modelo_id);
    };    
    
});

  • Afonso! Everything made a lot of sense and really worked. The only thing I still don’t understand why, I’m not getting the $Scope.marca and $Scope.modelo parameter in the formCtrl controller. Any idea why that is? The search result page is all right, I did a manual test with "#/search/35/2736" and all right, but when I do the form comes with values: "#/search/Undefined/Undefined".

  • Ops, I was inserting it into the wrong controller. I adjusted it to the correct controller and it’s working 100%. Thank you Afonso!

Browser other questions tagged

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