操纵NG-REPEAT的第一个元素

时间:2015-01-30 06:15:21

标签: javascript jquery angularjs

如果这是一个菜鸟问题,我很有想象力而且我很抱歉。我有一个由ng repeat渲染的列表,我想修改元素的第一次出现,该元素的particluar属性设置为“Y”。

<tr ng-repeat="player in player_history" >
<td>....</td>
<td>....</td>
</tr>


player_history = [
                        {
                            "value" : "10",
                            "description" : "rooney"
                            "award" : "y"      
                        },
                        {
                                "value" : "7",
                                "description" : "di maria"
                                "award" : "N"      
                        },
                        {
                                "value" : "1",
                                "description" : "de gea"
                                "award" : "N"      
                        },

                        {
                                "value" : "16",
                                "description" : "carrick"      
                                "award" : "Y"
                        },
                    ];

因此,对于第一次出现奖励为“Y”,我想对该元素进行一些修改。例如,在表格中显示结果,让我们说以粗体显示该特定条目。我仍然是一个新人,所以所有的帮助非常感谢。并且非常感谢你。

PS:我也在考虑让ng-repeat进行渲染,然后使用Jquery进行修改,因为我想要。但是到目前为止还没有尝试。再次感谢你。

3 个答案:

答案 0 :(得分:0)

使用'award'的值作为班级名称。

<tr ng-repeat="player in player_history" >
<td class = "{{player.award}}">....</td>
<td class = "{{player.award}}">....</td>
</tr>

并在CSS中按照您的喜好设置样式。

.Y {
   color:green;
   }
.N {
   color:red;
    }

跳这个有帮助!

答案 1 :(得分:0)

如果您想要一个特定的类用于样式目的:

<tr ng-repeat="player in player_history" ng-class="{isY: player.award === 'Y'}" >
  <td>....</td>
  <td>....</td>
</tr>

然后,你可以在tr.isY:first-child上应用你的CSS,不同地设置第一个。

现在,如果你想要一个完全不同的HTML元素,你应该先把它整理出来。所以在你的控制器中:

for (var i = 0; i < $scope.player_history.length; i++) {
    if ($scope.player_history[i].award === 'Y') {
        $scope.firstYAward = $scope.player_history[i];
        break;
    }
}

然后在你的HTML中:

<tr ng-repeat="player in player_history">
  <td ng-if="player === firstYAward">.. specific html for the first Y ..</td>
  <td ng-if="player !== firstAward"> .. regular html for the others .. </td>
</tr>

答案 2 :(得分:0)

可以有很多方法,但我在这里做的是获取第一个y的索引,然后在ng-repeat时检查它以使用ng-class标记具有特定类的元素。这对我有用,

HTML:

<table ng-controller="PlayerController">
    <tr ng-repeat="player in player_history">
        <td ng-class="(player.award=='y' || player.award=='Y') && $index == firstYFound ? 'firstY' : 'restAll'">{{player.description}}</td>
    </tr> 
</table>

AngularJs:

data = [
    {
        "value": "10",
        "description": "rooney",
        "award": "N"
    },
    {
        "value": "7",
        "description": "di maria",
        "award": "N"
    },
    {
        "value": "1",
        "description": "de gea",
        "award": "N"
    },
    {
        "value": "16",
        "description": "carrick",
        "award": "Y"
    }
];

function PlayerController($scope){
    $scope.player_history = data;
    for(var i = 0; i < $scope.player_history.length; i++){
        if($scope.player_history[i].award.toLowerCase() == "y"){
            $scope.firstYFound = i;
            break;
        }
    }
}

jsFiddle