gpt4 book ai didi

javascript - 使用 Angular 在 html 中打印 json 用户 ID

转载 作者:行者123 更新时间:2023-12-03 02:56:05 25 4
gpt4 key购买 nike

我只想打印用户 JSON 文件 {{item.id}} 的 id,但它不起作用,我不明白问题出在哪里。我认为问题出在 $http.get(urls).then() 但我不知道如何解决这个问题。

如果我像这样写 {{item}} 它就会起作用。

var app = angular.module("myApp", []);

app.controller("controller", function($scope, $http) {



var url = "https://jsonplaceholder.typicode.com/users";

$http.get(urls).then(function(response) {
$scope.users = response;
});

});
<!DOCTYPE html>
<html lang="en" ng-app="myApp">

<head>

<meta charset="utf-8">
<title>AngularJa | app</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
<script type="text/javascript" src="javascript/main.js"></script>

</head>

<body ng-controller="controller">

<table class="table table-striped">
<tr ng-repeat="item in users">
<td>{{item.id}}</td>
</tr>
</table>



</body>

</html>

最佳答案

urls更改为url,数据是响应对象的属性,名为data

var app = angular.module("myApp", []);

app.controller("controller", function($scope, $http) {

var url = "https://jsonplaceholder.typicode.com/users";

$http.get(url).then(function(response) {
//^^ remove "s"
$scope.users = response.data;
// ^^ add data property
});

});
<!DOCTYPE html>
<html lang="en" ng-app="myApp">

<head>

<meta charset="utf-8">
<title>AngularJa | app</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
<script type="text/javascript" src="javascript/main.js"></script>

</head>

<body ng-controller="controller">

<table class="table table-striped">
<tr ng-repeat="item in users">
<td>{{item.id}} - {{item.name}}</td>
</tr>
</table>


</body>

</html>

关于javascript - 使用 Angular 在 html 中打印 json 用户 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47613157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com