0

Hello I am new in angularjs I have following code, 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>

here i am getting data as

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

So it works perfect for 3 homes but the problem is I have four homes,and the fourth home is not yet rated so I need to show data-score='0' for that home. I tried as follow

<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>
3
  • {{avg.avg || 0 }} ? avg.avg must be undefined... so maybe you can test it in a fiddle printing {{ avg.avg === undefined }} Commented Mar 25, 2015 at 14:55
  • Yeah its undefined only Commented Mar 25, 2015 at 15:02
  • so it must work.fiddle it. and what's "ng-modal" ? i suppose it's a typo error (ng-model ?) Commented Mar 25, 2015 at 15:06

2 Answers 2

1

You can create a method.

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

And then in html

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

EDIT: Missed the () around avg.avg.

Sign up to request clarification or add additional context in comments.

1 Comment

No sir thats not entering in function only I tried putting alerts in if and else. One more thing to be keep in mind is the function u called at data-score which means according to my code that will come inside only when I have home_info_id and avg, so I need to check something reverse of it.
0
<div ng-repeat=' home in homeDetailInfo'>
  <div class="star" data-score="{{ getScore(home.home_info_id) }}"></div>
</div>

And then in your controller

$scope.getScore = function (homeInfoId) {
  var score = 0;

  homerating.forEach(function (rating) {
    if (rating.home_info_id === homeInfoId) {
      score = rating.avg;
    }
  }

  return score;
};

1 Comment

Great idea! thanks but you missed to complete that homerating.foreach(function(rating){ ... }) } but I got your trick. many thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.