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

Asked

Viewed 578 times

3

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

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

But I’m not getting it on my ng-repeat filter:

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

Does anyone have any idea how I can look into this? Because I have searched and have found only references to filters in input search engine.

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(); 
});
  • Show your controller "detail", so we can help you!

  • Ready Fernando, my controller is the one I posted.

  • Edited question, thanks for the comments. Any idea on solution of my problem Fernando?

1 answer

3


In your controller, you have access to route parameters via $routeParams. So it’s possible to store 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 the 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 the data access part as a service, but I believe this is the simplest given the code you already have.

  • 2

    Thanks bfavaretto, I made only a small change in this.scoreSelected = $routeParams.plate; for $Scope.scoreSelected = $routeParams.plate; to work as expected. Thank you very much!

Browser other questions tagged

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