search

Home  >  Q&A  >  body text

angular.js - angularjs的自定义过滤器如何给文字加颜色?

这是过滤器:

app.filter('ifLogin', function () {

    return function (target) {


        if (target == "0") {

            return "在职";

        } else {

            return "离职";

        }

    }

});

这是现实的部分:

<tr ng-repeat="x in datas">
                    <td>{{ x.id }}</td>
                    <td>{{ x.corp}}</td>
                    <td>{{ x.department }}</td>
                    <td>{{ x.status|ifLogin }}</td>
                </tr>

展示显示结果:

问题,如果让在职显示绿色,离职显示红色!!!??
angular有没有什么方便的做法吗?

ringa_leeringa_lee2741 days ago459

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-05-15 17:03:58

    For this requirement, it is not suitable to use filters for the color part. It is more convenient to use ng-class或者ng-style, for example:

    <tr ng-repeat="x in datas">
        <td>{{ x.id }}</td>
        <td>{{ x.corp}}</td>
        <td>{{ x.department }}</td>
        <td ng-style="{color: x.status === '0' ? 'green' : 'red'}">{{ x.status |ifLogin }}</td>
    </tr>

    reply
    0
  • Cancelreply