Setting value depending on response in AngularJs

2019-08-03 16:30发布

问题:

Let's say I have the following code:

<tr ng-repeat="transaction in $data">
    <td data-title="'Id'" sortable="id">{{transaction.id}}</td>
    <td data-title="'Player'" sortable="playerId">{{transaction.playerId}}</td>
    <td data-title="'Type'" sortable="type">{{transaction.type}}</td>
    <td data-title="'Date'" sortable="date">{{transaction.date | amDateFormat:'lll'}}</td>
    <td data-title="'Provider'" sortable="provider">{{transaction.paymentMethod}}</td>
    <td data-title="'Amount'" sortable="amount">{{transaction.amount | currency:'€ '}}</td>
    <td data-title="'Code'" sortable="code">{{transaction.code}}</td>
    <td data-title="'Status'" sortable="status">{{transaction.status}}</td>
</tr>

For "transaction.code" the response from the server is 'SRV_DPS'. How do I display that as "Deposit" in the browser?

回答1:

Use a custom filter:

JS

.filter('convertCode', function() {
  return function(code) {
    return (code === 'SRV_DPS') ? 'Deposit' : code;
  }
})

View

{{transaction.code | convertCode}}

Demo



回答2:

I believe you want:

<td data-title="'Code'" sortable="code">{{transaction.code == 'SRV_DPS' ? 'Deposit' : 'Other'}}</td>