Json filter in Angularjs

Asked

Viewed 615 times

1

I would like how I can filter this JSON by COD and bring only the chosen one.
I am bringing the value of the code by 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 in which he chose.
I am using Angularjs
Example:

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

Then, I would like to filter only through the COD column and bring and present on the screen the NAME.

2 answers

3


ANGULAR FILTER

To solve your problem just use the Filters of Angular, the following 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>
<div ng-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>

  • Yes, but I get the COD by the url, that is, the user has already chosen the item in the list so I would like to filter it to appear in the new page the selected item.

  • You treat this URL as? PHP?

  • No, I am using Ionic and angular, in case it would be the following process: The user has a list of options, he chooses one, and goes to another view, and in this other view wanted to just show the name of the chosen option. But based on your answer I managed to solve the problem here. Thank you very much! :)

0

For you to perform the filter the way you pointed 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)
                                });
}

Browser other questions tagged

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