gpt4 book ai didi

javascript - 当数组没有值可以在angularjs中比较时如何分配默认值

转载 作者:行者123 更新时间:2023-12-03 10:29:59 28 4
gpt4 key购买 nike

你好,我是 angularjs 的新手,我有以下代码,HTML

//here i have one more ng-repeat so comparing with this home.home_info_id to avgrating avg.home_inof_id
<div ng-repeat=' home in homeDetailInfo'>
<div ng-repeat="avg in homerating">
<div ng-if="avg.home_info_id==home.home_info_id">
<div class="star" ng-modal="avg" data-score="{{avg.avg}}"></div>
</div>
</div>

这里我获取的数据为

hallrating=[
{"home_info_id":"94","avg":"3.33333333333333"},
{"home_info_id":"119","avg":"4"},
{"home_info_id":"95","avg":"4.5"}
]

所以它非常适合 3 个房屋,但问题是我有四个房屋,而第四个房屋尚未评级,因此我需要显示该房屋的 data-score='0'。我尝试如下

<div ng-repeat=' home in homeDetailInfo'>
<div ng-repeat="avg in homerating">
<div ng-if="avg.home_info_id==home.home_info_id">
<div class="star" ng-modal="avg" data-score="{{avg.avg}}"></div>
</div>
<div ng-if="avg.home_info_id!=home.home_info_id && avg.avg!=''">
<div class="star" ng-modal="avg" data-score="0"></div>
</div>
</div>

not worked for me i also tried
<div ng-repeat=' home in homeDetailInfo'>
<div ng-repeat="avg in homerating">
<div ng-if="avg.home_info_id==home.home_info_id">
<div class="star" ng-modal="avg" data-score="{{avg.avg}}"></div>
</div>
<div ng-if="!avg.avg">
<div class="star" ng-modal="avg" data-score="0"></div>
</div>
</div>

最佳答案

您可以创建一个方法。

$scope.showScore = function(score) {
if (angular.isDefined(score)) {
return score;
} else {
return 0;
}
}

然后是html

<div class="star" ng-modal="avg" data-score="showScore(avg.avg)"></div>

编辑:错过了 avg.avg 周围的 ()。

关于javascript - 当数组没有值可以在angularjs中比较时如何分配默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29258888/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com