您好,我是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个房屋,但问题是我有4个房屋,第四个房屋尚未评级,因此我需要为该房屋显示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>
最佳答案
<div ng-repeat=' home in homeDetailInfo'>
<div class="star" data-score="{{ getScore(home.home_info_id) }}"></div>
</div>
然后在您的控制器中
$scope.getScore = function (homeInfoId) {
var score = 0;
homerating.forEach(function (rating) {
if (rating.home_info_id === homeInfoId) {
score = rating.avg;
}
}
return score;
};