gpt4 book ai didi

angularjs - 模板页面中的 ng-repeat 显示无数据

转载 作者:行者123 更新时间:2023-12-01 23:44:07 25 4
gpt4 key购买 nike

我正在尝试学习 Angular ,但我一直在尝试从 json 对象中重复一个 div。我已经确认返回了 json 对象,如果我只是将段落标记正确显示 myData 但当我检查 review.html 中的 html 时,我的 div 没有重复甚至显示。根据 jsonlint,我的 json 也是有效的。知道这里可能出了什么问题吗?

这里是index.html

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>All The Reviews</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular-route.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.css">
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.js"></script>
<script src="myScript.js"></script>
</head>

<body ng-app="myApp">
<div ng-controller="homeController">
<div>
<nav class="navbar navbar-inverse" role="navigation" style="padding-left:130px;">
<ul class="nav navbar-nav">
<li class="active"><a href="#/">Home<span class="sr-only">(current)</span></a></li>
<li><a href="#/reviews">Reviews</a></li>
<li><a href="#/create">Create</a></li>
</ul>
</nav>
</div>
<br/>
<div ng-view class="jumbotron"></div>
</div>
</body>
</html>

这是我的模板 reviews.html

<div style="padding-left:130px;padding-right:200px;">
<h1>Reviews</h1>
<p>{{myData}}</p>
<div ng-repeat="review in myData.reviews">{{review.title}}</div>
</div>

这是我的js文件

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


app.config(function($routeProvider){


$routeProvider
.when('/',{
templateUrl: 'home.html',
controller: 'homeController'
})
.when('/reviews',{
templateUrl: 'reviews.html',
controller: 'reviewsController'
});


});


app.controller('homeController',function($scope, $http){

});

app.controller('reviewsController',function($scope, $http){

$scope.myData={};

$http.get('myData.json').success(function(data) {
$scope.myData = data;
});

});

这是我的 json 数据

[{"products":[{"productId":"1","name":"Fluffy Eggs"},{"productId":"1","name":"Crispy Bacon"}]},
{"reviews":[{"reviewId":"1","title":"These are some nice eggs.","productId":"1","description":"Eggs that are just the right temp and extremely fluffy."},
{"reviewId":"2","title":"mmm, bacon","productId":"2","description":"it's pretty crispy. Maybe a little too crispy."}]},
{"ratings":[{"ratingId":"1","reviewId":"1","rating":"5"},
{"ratingId":"2","reviewId":"1","rating":"4"},
{"ratingId":"3","reviewId":"2","rating":"1"},
{"ratingId":"4","reviewId":"1","rating":"5"},
{"ratingId":"5","reviewId":"1","rating":"5"},
{"ratingId":"6","reviewId":"2","rating":"5"},
{"ratingId":"7","reviewId":"2","rating":"4"},
{"ratingId":"8","reviewId":"1","rating":"5"}]}]

感谢您的参与

最佳答案

您的 JSON 结构是一个数组,其中包含具有属性 productsreviewsratings 的三个对象。因此,您必须选择第二个项目来循环查看您的评论:

<div ng-repeat="review in myData[1].reviews">{{review.title}}</div>

See this plnkr

关于angularjs - 模板页面中的 ng-repeat 显示无数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30019985/

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