Show Item if True in Array Json

1

How do I show the content of a person if at least some of the items within the array has the status denied , this is my código .

index.html

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>Refinanciador</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
  <script src="app.js" charset="utf-8"></script>
</head>

<body ng-app="app">
      <div class="row" ng-controller="appController">
          <h1> FUTBOLISTAS </h1>
           <ul>
              <li ng-repeat="data in datos">
                  <p>Nombre: {{ data.nombre }} <br /> Apellido: {{ data.apellido }}</p> <br /> Estado: {{ data.status }}
              </li>
          </ul>
          <div ng-if="showPersonDenied" style="width: 100px; height: 100px; background-color: red; text-align: center;">
              Aca va una eprsona con estado denegado
          </div>
       </div>
 </body>
</html>

</body>

</html><br><br>

app.js

var app = angular.module("app", []);

app.controller("appController", function ($scope, $http) {
   //vamos a hacer uso de $http para obtener los datos
   $http.get('data.json').then(function (data) {
   //enviamos los datos a la vista con el objeto $scope
   $scope.datos = data.data;
   $scope.personStatus = $scope.datos;

   if ($scope.personStatus.length){
        for (var i = 0; i < $scope.personStatus.length ; i++) {
            $scope.showPersonDenied = $scope.personStatus[i].status === 'denied';
        }
    }

 });   
})<br><br>

data.json

[
   {
      "nombre": "James",
      "apellido": "Rodrigez",
      "status": "approved"

   },
   {
      "nombre": "Zlatan",
      "apellido": "Ibrahimovic",
      "status": "denied"

   },
   {
      "nombre": "Lionel",
      "apellido": "Messi",
      "status": "approved"

   }
]


the idea is that this $ scope.showPersonDenied variable is true if any of the items within the array has the status == 'denied'

    
asked by zerokira 26.02.2018 в 20:34
source

2 answers

3

If you want to list ONLY those with the denied status you can use $filter

$scope.personStatus = $scope.datos;
$scope.personDenied = $filter('filter')($scope.personStatus, { status: "denied" });

And you only make ng-repeat of your% $scope.personDenied like this:

<li ng-repeat="usuarios in personStatus" ng-show="personDenied.length > 0"> 

So you control that if there is none, the list with ng-show is not displayed.

    
answered by 26.02.2018 / 21:24
source
0

Well you just have to add a ng-if to validate the condition, it should look like this:

<li ng-repeat="data in datos" ng-if="data.status == 'approved'">...<li>

Here you can read a little more about ng-if: ng-if

    
answered by 26.02.2018 в 21:11