admin管理员组文章数量:1313112
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.
$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>
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.
$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>
Share
Improve this question
asked Jan 8, 2016 at 12:20
RRPANDEYRRPANDEY
2351 gold badge4 silver badges18 bronze badges
1
- 1 Take a look at this question, you could do something similar: stackoverflow./questions/16824853/… – Etse Commented Jan 8, 2016 at 12:25
4 Answers
Reset to default 4Create 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>
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>
You could use a second ng-repeat:
<span ng-repeat="idx in country.rating track by $index" class="glyphicon glyphicon-star"</span>
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
版权声明:本文标题:javascript - Adding Span iconsglyphicongraph in td based on conditionnumber using AngularJs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741941788a2406184.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论