我有一个用于产品的循环,效果很好。
<div ng-repeat="result in results track by result.uid">
<img ng-src="{{ result.img';" alt="{{ result.name }}"
title="{{ result.name }}" />
<p><span class="money">£{{ result.price | number:2 }}</span></p>
<p class="product-title">
<a ng-href="{{ result.url }}">{{ result.name }}</a>
</p>
</div>
在循环中,有一个具有JSON数据的对象。
{{result.colours}}
其中包含以下内容:
[
{
"id":866337128495,
"title":"Product Title",
"handle":"product-url",
"image":"/img.jpg",
"sku":"SKU001",
"name":"Product Name",
"type":"one_color",
"data":"#000000"
},
{
"id":866337128496,
"title":"Product Title2",
"handle":"product-url2",
"image":"/img2.jpg",
"sku":"SKU002",
"name":"Product Name 2",
"type":"one_color",
"data":"#000000"
}
]
我需要遍历这个,并尝试过:
<div ng-init="swatches=result.colours">
<div ng-if="swatches">
<div ng-repeat="(key,value) in swatches">
{{key}}:{{value}}
</div>
</div>
</div>
这只是返回:
0: [
{
"id":866337128495,
"title":"Product Title",
"handle":"product-url",
"image":"/img.jpg",
"sku":"SKU001",
"name":"Product Name",
"type":"one_color",
"data":"#000000"
},
{
"id":866337128496,
"title":"Product Title2",
"handle":"product-url2",
"image":"/img2.jpg",
"sku":"SKU002",
"name":"Product Name 2",
"type":"one_color",
"data":"#000000"
}
]
我是Angular的新手,但是我研究了很多实现此功能的可能性,但都没有做到。任何想法都将受到欢迎!
答案 0 :(得分:0)
Here is a link on how default parameters work in javascript
您快到了:
<div ng-if="swatches">
<div ng-repeat="data in swatches">
<div ng-repeat="(key,value) in data">
{{key}}:{{value}}
</div>
<hr>
</div>
</div>
答案 1 :(得分:0)
angular.module("app", [])
.run(function($rootScope) {
$rootScope.results = [
{
name: "Demo",
colors: [{
"id": 866337128495,
"title": "Product Title",
"handle": "product-url",
"image": "/img.jpg",
"sku": "SKU001",
"name": "Product Name",
"type": "one_color",
"data": "#000000"
},
{
"id": 866337128496,
"title": "Product Title2",
"handle": "product-url2",
"image": "/img2.jpg",
"sku": "SKU002",
"name": "Product Name 2",
"type": "one_color",
"data": "#000000"
}
]
}
]
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="app">
<div ng-repeat="result in results track by result.uid">
{{result.name}}
<div ng-init="swatches=result.colors">
<div ng-if="swatches">
<div ng-repeat="swatch in swatches">
<div ng-repeat="(key, value) in swatch">
{{key}}:{{value}}
</div>
<br>
</div>
</div>
</div>
</div>
</div>