1

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>

4
  • your json data is an array or object? Commented Dec 31, 2016 at 13:39
  • its a JSON object Commented Dec 31, 2016 at 13:42
  • then why are you using ng-repeat? Commented Dec 31, 2016 at 13:45
  • yes ng- repeat is not needed. Commented Dec 31, 2016 at 13:46

1 Answer 1

1

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>

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

7 Comments

what about $scope.names = response.data;or data.records;
of course, you can do that by fetching the data from your server... I don't have access to it to show you a demo :)
still im getting like that only.
do a console.log(response.data.record) inside the .then(function(){ to see how the data is coming from the url...
and also check if you have put ng-app and ng-controller on the html?
|

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.