Matieu
Matieu

Reputation: 39

Select precise data's value in JSON object using AngularJS

Currently new & working on a web app using AngularJS, using Nginx serv.

I want to select some precise values to put them in a html table.

I kept searching and never find an adequate solution.

Do you have an idea on how can I make this work?

Here's the code:

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
        <script>
        var app = angular.module('app', []);
        app.controller('myCtrl', function($scope, $http) {
          $http.get('variables.json').success(function(data) {
                 $scope.variables = data;
          });
        });
        </script>
    <link rel="stylesheet" type="text/css" href="../app/css/style.css">
  </head>
    <body ng-app="app" ng-controller="myCtrl" ng-init="variables">
      <ul>
        <li ng-repeat="var in variables">
          {{var.b}}
          {{var.a}}
        </li>
      </ul>
    </body>
</html>

JSON File:

 {
  "a":6084.0,
  "b":979
 }

I just put an example of the JSON file. My purpose is to display for example just the b or a value.

Upvotes: 0

Views: 115

Answers (3)

Jacopo Lanzoni
Jacopo Lanzoni

Reputation: 1316

The directive ngRepeat works with objects if the following syntax is used:

<div ng-repeat="(key, value) in myObj"> ... </div>

Upvotes: 0

Naghaveer R
Naghaveer R

Reputation: 2944

You can read as (key,value) in $variables and display value.

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

Demo link https://plnkr.co/edit/nTUOvyMHcDRdtSzyecLw?p=preview

Upvotes: 1

Dev
Dev

Reputation: 62

ng-repeat only works if the data in your json will be Array Since the data is the Object it will not find any element so it won't showing values .

change the Json as below:

[{
  "a":6084.0,
  "b":979
}]

Upvotes: 0

Related Questions