我想将 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.userid
和names.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/