[英]Show json data in table with angular
I just started looking at an old project, and I am stuck with a problem. 我刚刚开始查看一个旧项目,但遇到了问题。 I want to show some data in a table, but i forgot how it works with json objects and angular.
我想在表中显示一些数据,但是我忘记了它如何与json对象和angular一起工作。
I am getting data from an API that looks like this. 我从看起来像这样的API获取数据。
{"Search":[{"Title":"Not Another Teen Movie","Year":"2001","imdbID":"tt0277371","Type":"movie","Poster":"http://ia.media-imdb.com/images/M/MV5BODYyNTQyNzAzNF5BMl5BanBnXkFtZTgwNTA4ODYxMTE@._V1_SX300.jpg"},{"Title":"Not Just Another 8 Teen Movie","Year":"2003","imdbID":"tt0381457","Type":"movie","Poster":"N/A"},{"Title":"Not Just Another 8 Teen Movie 2","Year":"2003","imdbID":"tt0397579","Type":"movie","Poster":"N/A"},{"Title":"Not Just Another 8 Teen Movie 3","Year":"2004","imdbID":"tt0408045","Type":"movie","Poster":"N/A"}],"totalResults":"4","Response":"True"}
Then i am scoping the data like this 然后我像这样对数据进行范围界定
$scope.AllMoviesFound = data;
And then i want to show all the different titles in my table 然后我想在表格中显示所有不同的标题
<table class="table table-striped table-hover">
<thead>
<tr class="success">
<th>
<h3>Title</h3></a>
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in AllMoviesFound">
<td><h4>{{item.Title}}</h4></td>
</tr>
</tbody>
</table>
But i can not quite get this to work. 但是我不能完全使这个工作。 What am i missing in my table?
我桌子上想念什么?
$scope.AllMoviesFound = data.Search
You can use 您可以使用
var response = '{"Search":[{"Title":"Not Another Teen Movie","Year":"2001","imdbID":"tt0277371","Type":"movie","Poster":"http://ia.media-imdb.com/images/M/MV5BODYyNTQyNzAzNF5BMl5BanBnXkFtZTgwNTA4ODYxMTE@._V1_SX300.jpg"},{"Title":"Not Just Another 8 Teen Movie","Year":"2003","imdbID":"tt0381457","Type":"movie","Poster":"N/A"},{"Title":"Not Just Another 8 Teen Movie 2","Year":"2003","imdbID":"tt0397579","Type":"movie","Poster":"N/A"},{"Title":"Not Just Another 8 Teen Movie 3","Year":"2004","imdbID":"tt0408045","Type":"movie","Poster":"N/A"}],"totalResults":"4","Response":"True"}'
;
var JsonObject= JSON.parse(response);
//OR //要么
$(jQuery.parseJSON(JSON.stringify('your string')['search'])).each(function() {
var title = this.title;
//do stuff
});
Writing $scope.AllMoviesFound = data;
编写
$scope.AllMoviesFound = data;
means for instance you have a candy bag you try to eat the bag, that won't works. 举例来说,这意味着您有一个糖果袋,您想吃它,那是行不通的。
Here your movies data are in a 'box' called data. 在这里,您的电影数据位于名为“数据”的“框”中。 So you have to take movies inside this box to work on it.
因此,您必须在此框内拍电影才能对其进行处理。
$scope.AllMoviesFound = data.Search
means you take Search from data and put it in your scope. 表示您从数据中获取搜索并将其放入您的范围。 You can now iterate it.
您现在可以对其进行迭代。
Moreover you cannot use ng-repeat as it with objects, it works only with arrays. 此外,您不能将ng-repeat与对象一起使用,它仅适用于数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.