如何更改 table 数据的值取决于 ng-repeat | 中的条件。 AngularJs

How to change the value of a table data depends on a conditio inside the ng-repeat | AngularJs

我有 table 个订单,根据订单类型我想更改 td 的值。以下是我的看法:

<tr ng-repeat="invoice in allInvoices">
    <td>{{invoice.order_type}}</td>
    <td>{{invoice.order_date}}</td>
    <td>{{invoice.totalPrice}}</td>
    <td>{{invoice.client_name}}</td>
</tr>

我试过了:

<td ng-if="invoice.order_type='M'">Mobile</td>
<td ng-if="invoice.order_type='A'">Accessories</td>

但这不适合我的 table 视图,因为它会显示两个 td,而我想继续显示一个 td 但更改它的值取决于顺序类型。

提前致谢!

尝试

<td ng-if="invoice.order_type==='M'">Mobile</td>
<td ng-if="invoice.order_type==='A'">Accessories</td>

= 是赋值,其中 ng-if 里面总是有条件语句,即使用 ===

使用ternary operator:

<td>{{invoice.order_type=='M'?'Mobile':'Accessories'}}</td>

或使用 property accessor 进行查找:

<td>{{orderTypeName[invoice.order_type]}}</td>
$scope.orderTypeName = {
    'M': 'Mobile',
    'A': 'Accessories',
};

这避免了将 ng-if 与后续的子作用域一起使用。

尝试比较运算符 == 而不是赋值运算符 =

<td ng-if="invoice.order_type=='M'">Mobile</td>
<td ng-if="invoice.order_type=='A'">Accessories</td>

你可以只使用像

这样的表达式
{{invoice.order_type == 'M' ? 'Mobile' : invoice.order_type == 'A' ? 'Accesories' : 'other'}}

方案如下:

{{expression ? ifTrue : otherExpression ? ifTrue : anotherExpression ? ifTrue : ifFalse}}