用角度显示表中的json数据

时间:2016-02-13 19:15:31

标签: javascript angularjs json

我刚开始看一个旧项目,我遇到了问题。 我想在表中显示一些数据,但我忘了它如何与json对象和角度一起工作。

我从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"}

然后我确定了这样的数据

$scope.AllMoviesFound = data;

然后我想在表格中显示所有不同的标题

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

但我无法让这个工作。我的桌子上缺少什么?

3 个答案:

答案 0 :(得分:1)

$scope.AllMoviesFound = data.Search

答案 1 :(得分:0)

您可以使用

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
    });

答案 2 :(得分:-1)

$scope.AllMoviesFound = data;意味着你有一个你尝试吃袋子的糖果袋,这是行不通的。

此处您的电影数据位于名为“数据”的“框”中。所以你必须在这个盒子里拍电影才能使用它。

$scope.AllMoviesFound = data.Search

表示您从数据中搜索并将其放入您的范围。你现在可以迭代它。

此外,你不能像对象那样使用ng-repeat,它只适用于数组。