3

I would like to display complex json with bollean values, strings and even with arrays. How can I do that?

$scope.options = {    
    "option_1" : "some string",    
    "option_2" : true,
    "option_3" : [1.123456789, 0.123548912, -7.156248965],
    "option_4" : null,    
    "option_5" : [1234.45678, 75.142012] 
} 

I use something like this : but I have problem with arrays :

<ul ng-repeat="(key, value) in options">     
    <li>{{key}}</li>     
    <span>{{value}}</span> 
</ul> 

I would like display something like table with keys like headings and values under appropriate keys like this :

option_1     option_2    option_3       ... 
some string  true        1.123456789                          
                         0.123548912                         
                        -7.156248965
3

1 Answer 1

4

It should be like this.

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

app.controller("myCtrl", function($scope) {
  $scope.isArray = angular.isArray;
  $scope.options = {
    "option_1": "some string",
    "option_2": true,
    "option_3": [1.123456789, 0.123548912, -7.156248965],
    "option_4": null,
    "option_5": [1234.45678, 75.142012]
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="myCtrl">
    <table class="table table-striped table-bordered table-hover">
      <thead>
        <tr>
          <th ng-repeat="(key, value) in options">
            {{ key }}
          </th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td ng-repeat="(key, value) in options">
            {{isArray(value) ? '': value}}
            <table ng-if="isArray(value)">
              <tr ng-repeat="v in value">
                <td>
                  {{v}}
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </tbody>
    </table>

  </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.