Eni*_*aRM 9 filter angularjs angularjs-ng-repeat
我有一个包含数字和值的文本混合的对象.numbers当它是一个数字(显然)时,我想将过滤器应用于对象的值.但是当它不是一个数字时,我会好起来只是吐出字符串.应用于| number值格式化数字,但将字符串值保留为空(毕竟,它们不是数字).
我猜它必须是一个自定义过滤器(我还需要制作).有没有办法在做HTML时只在HTML中做ng-repeat?
<table>
<tr ng-repeat="(metric, metricData) in data">
<td>{{metric}}</td>
<td>{{metricData | number}}</td>
</tr>
</table>
$scope.data = { name:"this is the name",
score:48
outcome:"as expected",
attendance:820,
total:212.34
};
Run Code Online (Sandbox Code Playgroud)
Dan*_*Dan 18
以下是来自@callmekatootie使用ng-if(v1.1.5)的答案的备用版本:
<table>
<tr ng-repeat="(metric, metricData) in data">
<td>{{metric}}</td>
<td ng-if="isNumber(metricData)">{{metricData | number}}</td>
<td ng-if="!isNumber(metricData)">{{metricData}}</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
这样做的好处是只对数字元素运行过滤器.在这种情况下,这可能没什么好处,但在其他更复杂的过滤器情况下可能会有用.要回答关于内置的其他问题angular.isNumber,@ callmekatootie会在scope函数中使用它isNumber,它只是在视图中使用内置函数的包装器.
| 归档时间: |
|
| 查看次数: |
19670 次 |
| 最近记录: |