如何使用不同的密钥获取JSON中的特定元素

时间:2018-04-24 10:43:46

标签: angularjs angular typescript ionic-framework

如果我有JSON:

var responses = {customer : {results: 2938; id: 9283}, bredesh : {results: 2938; id: 248}    };

我需要在我的ltml中做一个if:

<div ng-if="response.(customer and bredesh and all new element (PRobleme is here) ).id=='a'" class="form-group">

1 个答案:

答案 0 :(得分:1)

您可以尝试列出对象元素的所有,并使用ng-if选择特定的元素。要分别列出键和值,请使用ng-repeat="(key,value) in your_array"语法。这是一个小型演示:

&#13;
&#13;
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.responses = {
    customer: {
      results: 2938,
      id: 9283
    },
    bredesh: {
      results: 2938,
      id: 248
    }
  }
  $scope.id = 248;
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">

  <div ng-repeat="(key, value) in responses">
    <div ng-if="responses[key].id==id" class="form-group">
      {{key}} - {{value}}
    </div>
  </div>
  <button ng-click="id=248">Select ID: 248</button>
  <button ng-click="id=9283">Select ID: 9283</button>

</div>
&#13;
&#13;
&#13;

否则您需要手动选择它:responses.customer.idresponses.bredesh.id