javascript - 使用 AngularJs 根据条件/数字在 td 中添加 Span 图标/字形/图形

标签 javascript jquery html css angularjs

我正在尝试在国家/地区之前使用 td 中的 Angular 添加跨度图标/字形图标,我使用了以下代码来执行此操作,但我需要动态地做更好的方法。 需要你的帮助。

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>

最佳答案

创建一个函数来获取星号数组 然后在 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>

关于javascript - 使用 AngularJs 根据条件/数字在 td 中添加 Span 图标/字形/图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34677032/

相关文章:

javascript - 如何实现点从a到b的移动过渡

html - 为什么带背景的 <a> 在 IE 中消失,但在 firefox 中没问题

javascript - 我的 javascript 计算器没有保存值

javascript - 如何为页面的特定部分包含 CSS

javascript - 向 BIRT 元素文本添加自定义样式

javascript - JQuery 使用 .find 禁用 Radio 输入元素

javascript - node.js mongodb - collection.find().toArray(callback) 返回空

jquery - Ajax/jQuery 问题

jQuery 脚本设置 div 高度似乎只运行一次然后停止

javascript - Jquery 添加表单,适用于 JSFiddle,但在生产中不起作用