Rahul Pawar
Rahul Pawar

Reputation: 169

How to show specific JSON object key on condition basis using angularjs

Using below JSON object need to show key.

{
   'fieldLabel': 'LABEL',
   'fieldName': 'TEST',
   'fieldKey': 'TEST2'
}

Below are the conditions: 1. If got fieldName, fieldLabel, and fieldKey then need to show fieldLabel 2. If only received fieldLabel and fieldKey then need to show fieldLabel 3. If only received fieldKey and fieldName then need to show fieldName

Upvotes: 1

Views: 156

Answers (1)

shaunhusain
shaunhusain

Reputation: 19748

{{object.fieldLabel?object.fieldLabel:object.fieldName}}

^^ should work assuming you're talking about binding that data into the view (using ternary operator, if object.fieldLabel, then print object.fieldLabel else print object.fieldName).

angular.module('myapp', [])
  .controller('MyCtrl', function($scope){
    $scope.objects = [
      {fieldLabel: 'I got a label'},
      {fieldName: 'I only have a name :('}
    ] 
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.7/angular.js"></script>
<div ng-app="myapp" ng-controller="MyCtrl">
  <div ng-repeat="object in objects">
  {{object.fieldLabel?object.fieldLabel:object.fieldName}}
  </div>
</div>

Upvotes: 1

Related Questions