在ng-repeat angular中格式化列值

Format column value in ng-repeat angular

本文关键字:格式化 ng-repeat angular      更新时间:2023-09-26

我是angular的新手,我有一个问题。我在数组中有一个值是1或0。当我在表中显示这些时,如果它是0,我想显示No,如果它是1,我想显示Yes。

我有这个表,Active显示1或0。

<table class="table table-striped table-bordered"        id="tableA">
                    <thead>
                                   <tr>
                                                       <th>ID</th>
                                                       <th>Namn</th>
                                                       <th>Active</th>
                                   </tr>
                </thead>
                <tbody>
                                   <tr ng-repeat="item in dRows">
                                                       <td>{{item.ID}}</td>
                                                       <td>{{item.Name}}</td>
                                                       <td>{{(item.Active)}}</td>
                                   </tr>
                </tbody>

我怎样才能把它改成Yes和No呢?请帮忙,谢谢

如果你使用的是Angular 1.1.5或更高版本,你可以使用条件运算符(?:):

<td>{{(item.Active == 1 ? 'Yes':'No')}}</td>

另一种方法是使用过滤器。首先,将过滤器添加到当前模块

app.filter('YesNo', function () {
    return function (num) {
        return num === 1 ? 'Yes' : 'No';
    };
});

然后使用

 <td>{{(item.Active | YesNo)}}</td>