2

I am trying to add span icons/glyph-icon using angular in td before country, i have used the following code to do so but i need to do more nicer way dynamically. need your help.

enter image description here

    $scope.countries = [
        {name: 'Australia', rating: 0, other: "lorem ipsum"},
        {name: 'India', rating: 1, other: "lorem ipsum"},
        {name: 'Canada', rating: 3, other: "lorem ipsum"},
        {name: 'Mexico', rating: 2, other: "lorem ipsum"},
        {name: 'UK', rating: 4, other: "lorem ipsum"},
        {name: 'Germany',rating: 2, other: "lorem ipsum"},
        {name: 'USA',rating: 1, other: "lorem ipsum"}
    ];

<tr ng-repeat="country in countries">
    <td>
        <span ng-if="country.rating == 1">
                <span class="glyphicon glyphicon-star"></span>
         </span>
        <span ng-if="country.rating == 2">
                <span class="glyphicon glyphicon-star"></span>
                <span class="glyphicon glyphicon-star"></span>
         </span>
        <span ng-if="country.rating == 3">
            <span class="glyphicon glyphicon-star"></span>
            <span class="glyphicon glyphicon-star"></span>
            <span class="glyphicon glyphicon-star"></span>
      </span>
        <span ng-if="country.rating == 4">
        <span class="glyphicon glyphicon-star"></span>
            <span class="glyphicon glyphicon-star"></span>
            <span class="glyphicon glyphicon-star"></span>
            <span class="glyphicon glyphicon-star"></span>
            <span class="glyphicon glyphicon-star"></span>
      </span>
        <span>
            {{ country.name}}
        </span>
    </td>
    <td>{{country.other}}</td>
</tr>
1

4 Answers 4

4

Create a function to get an array of star numbers then use it on ng-repeat

$scope.getArrayNumber = function(num) {
   return new Array(num);   
};


<tr ng-repeat="country in countries">
   <td>
      <span ng-repeat="star in getArrayNumber(country.rating) track by $index">
          <span class="glyphicon glyphicon-star"></span>
      </span>
      <span>
          {{country.name}}
      </span>
   </td>
</tr>
Sign up to request clarification or add additional context in comments.

2 Comments

koga. i am getting only star for India and USA, no icons for other countries
Add track by $index on ng-repeat
4

Based on the answer provided by @koga, another way to do is not using the track by:

$scope.getArrayNumber = function(num) {
    var array = [];
    for (var i = null; i < num; i++) {
        array.push(i);
    };
   return array;   
};

<tr ng-repeat="country in countries">
   <td>
      <span ng-repeat="star in getArrayNumber(country.rating)">
          <span class="glyphicon glyphicon-star"></span>
      </span>
      <span>
          {{country.name}}
      </span>
   </td>
</tr>

Comments

2

You could use a second ng-repeat:

<span ng-repeat="idx in country.rating track by $index" class="glyphicon glyphicon-star"</span>

2 Comments

country.rating is a number not an array. ng-repeat works with a number? stackoverflow.com/questions/16824853/…
does this work when country.rating is a number and not a collection?
1

Here is a more easy solution. Create some CSS that generates the stars :

.star {
    color: orange;
}
.star1:before {
    content : '★'
}
.star2:before {
    content : '★★'
}

...etc. Then use a ng-class function to set the correct star according to the rating :

<tr ng-repeat="country in countries">
    <td>
        <span ng-class="stars(country.rating)"></span>
        <span>
            {{ country.name}}
        </span>
    </td> 
    <td>{{country.other}}</td>
</tr>
</table>
$scope.stars = function(rating) {
   return 'star star'+rating;
}

working demo -> http://plnkr.co/edit/h3JsormYIZ6th3Kvblh5?p=preview

enter image description here

Comments

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.