我正在尝试学习角度,并且我试图从json对象重复div。我已经确认正在返回json对象,如果我只是将段落标记正确显示myData但我的div不重复甚至在我检查review.html中的html时显示。根据jsonlint,我的json也是有效的。知道这里有什么不对吗?
这是index.html
u
这是我的模板reviews.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>
这是我的js文件
<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>
这是我的json数据
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;
});
});
感谢您的所有输入
答案 0 :(得分:3)
您的JSON结构是一个包含三个对象的数组,其中包含属性产品,评论和评级。因此,您必须选择第二项来循环评论:
<div ng-repeat="review in myData[1].reviews">{{review.title}}</div>
答案 1 :(得分:0)
将你的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"}]}