如何根据复杂的方法设置类名?

时间:2014-08-09 10:31:14

标签: angularjs

我想通过调用方法来设置div的类名,因为这里的类名很复杂,我该怎么做?

<div ng-repeat="row in rows">
    <div ng-class="here i want to set class by calling method getClass(row)">...</div>
</div>

js code:

$scope.getClass = function(r) {
    if(r.id == 1)
        return "some-class";
    else if(r.id == 2)
        return "some-better-class";
    else if(r.id == 3)
        return "some-more-better-class";
    ... and so on
};

2 个答案:

答案 0 :(得分:1)

只是:

<div ng-repeat="row in rows">
  <div class="some" ng-class="getClass(row)">...</div>
</div>

演示 Fiddle

答案 1 :(得分:0)

如果您只想根据行号设置行的样式,可以使用有角度的ng-repeat&#39; $ index&#39;属性而不是函数:

<div ng-controller="fessCntrl">
    <div ng-repeat="row in rows">
        <div ng-class="'row'+$index">Name: {{row.name}}, Index: {{$index}}</div>
    </div>
</div>

和CSS:

.row0 {
    background-color: red;
}
.row1 {
    background-color: blue;
}
.row2 {
    background-color: yellow;
}

小提琴: http://jsfiddle.net/fess81/oj3xg5vb/