Javascript 如何根据数组中类别的不同类型使用ng repeat
我有这样的样本数据数组 让我们关注描述码 您可以在该数组中看到包含3个对象,但描述有两种类型(description1和description2) 我需要根据这个描述码生成div 在这种情况下,我需要两个div s description1和description2 我该怎么做Javascript 如何根据数组中类别的不同类型使用ng repeat,javascript,html,angularjs,Javascript,Html,Angularjs,我有这样的样本数据数组 让我们关注描述码 您可以在该数组中看到包含3个对象,但描述有两种类型(description1和description2) 我需要根据这个描述码生成div 在这种情况下,我需要两个div s description1和description2 我该怎么做 var apps = [ { name: 'Azurite', description: "description1", shine: 8, price: 110.50, rarity: 7, c
var apps = [
{
name: 'Azurite',
description: "description1",
shine: 8,
price: 110.50,
rarity: 7,
color: '#CCC',
faces: 14,
images: [
"img/01.png",
"img/02.png",
"img/03.png"
],
reviews: [{
stars: 5,
body: "I love this gem!",
author: "joe@example.org",
createdOn: 1397490980837
}, {
stars: 1,
body: "This gem sucks.",
author: "tim@example.org",
createdOn: 1397490980837
}]
},
{
name: 'Bloodstone',
description: "description2",
shine: 9,
price: 22.90,
rarity: 6,
color: '#EEE',
faces: 12,
images: [
"img/04.png",
"img/05.png",
"img/06.png"
],
reviews: [{
stars: 3,
body: "I think this gem was just OK, could honestly use more shine, IMO.",
author: "JimmyDean@example.org",
createdOn: 1397490980837
}, {
stars: 4,
body: "Any gem with 12 faces is for me!",
author: "gemsRock@example.org",
createdOn: 1397490980837
}]
},{
name: 'sample ',
description: "description1",
shine: 9,
price: 22.90,
rarity: 6,
color: '#EEE',
faces: 12,
images: [
"img/07.png",
"img/08.png",
"img/09.png"
],
reviews: [{
stars: 3,
body: "I think this gem was just OK, could honestly use more shine, IMO.",
author: "JimmyDean@example.org",
createdOn: 1397490980837
}, {
stars: 4,
body: "Any gem with 12 faces is for me!",
author: "gemsRock@example.org",
createdOn: 1397490980837
}]
}]
您需要类似于
groupBy
过滤器的东西,实现一个您可以使用的过滤器。只需将此模块添加到应用程序依赖项(angular.module('yourAppName',['angular.filter']
)中,并在ng repeat
表达式中使用它:
<div ng-repeat="(description, apps) in apps | groupBy : 'description'"></div>
Plunker:您也可以通过使用forEach函数为控制器中主数组的每个描述创建不同的数组,然后通过HTML中的ng repeat循环,而无需使用任何额外的插件 这是正在工作的plunker
希望这也有帮助你是说html根据对象的描述不同而不同吗?这正是我需要的