Swapna
Swapna

Reputation: 403

unable to get data into plain text (table) from json data using angular js

I want to show json data into a table, but its showing the result as below shown. How to show json data in table in angular js.

userid  {{x.userid}}
status  {{x.status}} 

Desired output:

userid 0000acfffe731122
status 3

Json data:

{  
   "userid":"0000acfffe731122",
   "status":3
}

<table>
    <tr>
        <td>
            device id
        </td>
        <td>
            {{names.userid}}
        </td>
    </tr>
    <tr>
        <td>
            device status
        </td>
        <td>{{names.status}}</td>
    </tr>
</table>

<script>
 var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http.get('https://example.com/abc', {
headers: { 'Authorization': 'Basic a2VybmVsc==' }
})
.then(function(response) {
    $scope.names = response.data;
});
});
</script>

Upvotes: 1

Views: 93

Answers (1)

kukkuz
kukkuz

Reputation: 42352

Your names object must be an array if you have to use ng-repeat, as that is what it is there for:

[{
  "userid": "0000acfffe731122",
  "status": 3
}];

See Demo below:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {

  $scope.names = [{
    "userid": "0000acfffe731122",
    "status": 3
  }];

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<table ng-app="myApp" ng-controller="myCtrl">
  <tr ng-repeat="x in names">
    <td>user id -</td>
    <td>{{x.userid}}</td>
  </tr>
  <tr ng-repeat="x in names">
    <td>status -</td>
    <td>{{x.status}}</td>
  </tr>
</table>

If it can't be an array you can use names.userid and names.status - see demo below:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {

  $scope.names = {
    "userid": "0000acfffe731122",
    "status": 3
  };

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<table ng-app="myApp" ng-controller="myCtrl">
  <tr>
    <td>user id -</td>
    <td>{{names.userid}}</td>
  </tr>
  <tr>
    <td>status -</td>
    <td>{{names.status}}</td>
  </tr>
</table>

Upvotes: 1

Related Questions