我正在学习Angular并且有一些酒店房价。但是,如果您停留的时间超过1晚,则NightlyRate中返回的数据将位于数组中,否则它将位于对象中。
我不确定如何处理这个问题?我可以在视图中内联,还是应该在控制器或模型中执行此操作?
如果超过1晚,我需要添加ng-repeat
如果1晚
{{roomPrices.NightlyRatesPerRoom.NightlyRate['@baseRate']}}
"NightlyRatesPerRoom": {
"@size": "1",
"NightlyRate": {
"@baseRate": "154.55",
"@rate": "154.55",
"@promo": "false"
}
},
如果超过1晚
<div ng-repeat="roomPrice in roomPrices.NightlyRatesPerRoom.NightlyRate">
<div class="row">
<div class="col-xs-8">
<p>Night {{$index+1}}:</p>
</div>
<div class="col-xs-4">
<p class="text-xs-right">${{roomPrice['@baseRate']}}</p>
</div>
</div>
</div>
"NightlyRatesPerRoom": {
"@size": "3",
"NightlyRate": [
{
"@baseRate": "154.55",
"@rate": "154.55",
"@promo": "false"
},
{
"@baseRate": "154.55",
"@rate": "154.55",
"@promo": "false"
},
{
"@baseRate": "154.55",
"@rate": "154.55",
"@promo": "false"
}
]
},
答案 0 :(得分:-1)
您可以在下面看到...以确定它是对象还是数组
var objnight = roomPrices.NightlyRatesPerRoom.NightlyRate;
if(Object.prototype.toString.call(objnight).toString() == "[object Array]"){
alert("array")
}else if( Object.prototype.toString.call(objnight).toString()=="[object Object]"){
alert(object)
}