javascript - 无法使用 Angular js将数据从json数据获取到纯文本(表)

标签 javascript jquery angularjs json

我想将 json 数据显示到表中,但它显示的结果如下所示。如何在 Angular JS 中的表中显示 json 数据。

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

期望的输出:

userid 0000acfffe731122
status 3

Json 数据:

{  
   "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>

最佳答案

如果您必须使用 ng-repeat,您的 names 对象必须是一个数组,因为这就是它的用途:

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

请参阅下面的演示:

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>

如果它不能是一个数组,您可以使用names.useridnames.status - 请参阅下面的演示:

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>

关于javascript - 无法使用 Angular js将数据从json数据获取到纯文本(表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41408760/

相关文章:

JavaScript 正则表达式匹配忽略文字点

javascript - 使用 Javascript 复制到剪贴板

javascript - 多图片src路径切换

javascript - 具有动态数据集的动态 Javascript 多线图表

javascript - 在 JSP 或 Javascript 中获取标签的值

jquery - 将随机 jquery 背景颜色更改为重复顺序?

jquery - 如何更改 Jquery 中动态生成元素的 HTML 内容

javascript - 使用 Javascript 将 HTML 单元格拖到表格上

javascript - 如何在用户选择其他选项时删除选项

javascript - 如何检查对象名称?