我是一名Angular JS初学者,这可能是一个非常愚蠢的问题,但我发现这很困难。我有以下代码用角度js编写,以显示带有类别的产品列表。
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"> </script>
<script>
var myApp = angular.module("myApp",[]);
myApp.controller('myController',function($scope){
$scope.welcome = "WELCOME TO STORE"
$scope.products =
{
"ProductDetails": [
{
"category": "Favorites",
"data": [
{
"price": "10",
"description": "Favorites product1 demo description",
"name": "Demo product1"
},
{
"price": "13",
"description": "Favorites product2 demo description",
"name": "Demo product2"
}
]
},
{
"category": "Stationary",
"data": [
{
"price": "10",
"description": "Favorites product1 demo description",
"name": "Demo product1"
},
{
"price": "13",
"description": "Favorites product2 demo description",
"name": "Demo product2"
},
{
"price": "13",
"description": "Favorites product2 demo description",
"name": "Demo product2"
}
]
},
{
"category": "Food",
"data": [
{
"price": "10",
"description": "Favorites product1 demo description",
"name": "Demo product1"
},
{
"price": "13",
"description": "Favorites product2 demo description",
"name": "Demo product2"
},
{
"price": "13",
"description": "Favorites product2 demo description",
"name": "Demo product2"
}
]
}
]
}
});
</script>
</head>
<body ng-app="myApp">
<div ng-controller="myController">
<h1>{{welcome}}</h1>
<ul>
<li ng-repeat="categories in products.ProductDetails">
<h3> {{categories.category}}</h3>
<ul>
<li ng-repeat="item in products.ProductDetails.data"></li>
<p>{{item.name}}</p>
<p>{{item.description}}</p>
<p>{{item.price}}</p>
</ul>
</li>
</ul>
</div>
</html>
</body>
我正在使用ng-repeat来遍历项目列表并显示它。我对使用多个ng-repeat以及如何绑定数据感到困惑。我需要类似于下图的输出。
提前致谢。
答案 0 :(得分:2)
您可以将具有ng-repeat的元素视为具有自己的局部范围。可以把它想象成{}中的一段代码,用于if语句或类似的东西。请注意下面的结束li
标记,您必须位于li
标记创建的块内,才能引用item
。
您还想重复categories.data
<li ng-repeat="item in categories.data">
<p>{{item.name}}</p>
<p>{{item.description}}</p>
<p>{{item.price}}</p>
</li>
答案 1 :(得分:1)
替换
ng-repeat="item in products.ProductDetails.data"
要
ng-repeat="item in categories.data"
答案 2 :(得分:0)
这是问题
<ul>
<li ng-repeat="categories in products.ProductDetails">
<h3> {{categories.category}}</h3>
<ul>
<li ng-repeat="item in categories.data"></li>//Set Categories
<p>{{item.name}}</p>
<p>{{item.description}}</p>
<p>{{item.price}}</p>
</ul>
</li>
</ul>