Filter in Json in AngularJS

1

I would like to filter this JSON by COD and bring only the chosen one. I'm bringing the value of the code through the url and would like to filter only to display the name of the chosen option. NOTE: The user will not enter the value because it was in a list on the previous screen that he chose. I'm using AngularJS
Example:

{"COD":"15","NOME":"14.01 Histórico Escolar."},
{"COD":"16","NOME":"14.02 Histórico Escolar - Regime de Urgência"}

So, I would just filter through the COD column and bring up and display the NAME.

    
asked by anonymous 10.02.2017 / 16:32

2 answers

3

ANGULAR FILTER

To solve your problem, simply use the Filters of the Angle, here's an example:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.valores =  [
      {
        "COD":"15",
        "NOME":"14.01 Histórico Escolar."
      },
      {
        "COD":"16",
        "NOME":"14.02 Histórico Escolar - Regime de Urgência"
      }];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><divng-app="myApp" ng-controller="myCtrl">
  <input type="text" placeholder="Buscar por ID" ng-model="filtro.COD">
  <div ng-repeat="x in valores | filter: filtro">{{x.NOME}}</div>
  
</div>
    
10.02.2017 / 16:41
0

For you to perform the filter the way you pointed it can use the same pure javascript

$scope.valores =  [
  {
    "COD":"15",
    "NOME":"14.01 Histórico Escolar."
  },
  {
    "COD":"16",
    "NOME":"14.02 Histórico Escolar - Regime de Urgência"
  }]

$scope.filtrarCodigo(codigo){
   return $scope.valores.filter(function(item){ 
                                    return (item.COD == codigo)
                                });
}
    
22.02.2017 / 22:51