Filter in ng-repeat filter the parameter I passed in get

3

Hello, I am passing a parameter through my route and need to retrieve records with this parameter through an ng-repeat.

$routeProvider.when("/detalhe/:placa", {
    templateUrl : 'view/detalhe.html',
    controller  : 'detalheCtrl'
});

But I'm not able to retrieve it from my filter within ng-repeat:

<div ng-repeat="item in items | filter: {placa: ???}">
   {{item.nome}}
</div>

Does anyone have any idea how I can search for it? For I have been researching and I have only found references to input search field filters.

This is my controller:

angular.module("myApp").controller("detalheCtrl", function($scope, $http, estoqueAPI) {
$scope.loading = true;

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

carregarEstoque(); 
});
    
asked by anonymous 17.07.2015 / 16:13

1 answer

3

On your controller, you have access to the route parameters through $routeParams . So you can save this as a model, and use its reference in the filter:

angular.module("myApp").controller("detalheCtrl", function($scope, $http, $routeParams, estoqueAPI) {
    $scope.loading = true;

    this.placaSelecionada = $routeParams.placa;

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

    carregarEstoque(); 
});

And ng-repeat would look like this:

<div ng-repeat="item in items | filter: {placa: placaSelecionada}">
   {{item.nome}}
</div>

There are other solutions, such as separating all data access as a service, but I think this is the simplest given the code you already have.

    
17.07.2015 / 16:56