Source JS du filtre:
1 2 3 4 5 6 7 8 9 |
// Is there a way to count the number of keys in a json object directly in angular expressions? // https://stackoverflow.com/questions/33520317/is-there-a-way-to-count-the-number-of-keys-in-a-json-object-directly-in-angular app.filter('numKeys', function() { return function(json) { var keys = Object.keys(json) return keys.length; } }); |
Utilisation qui en est faite dans le template:
Pour l’objet JSON lineData.details.config
, numKeys
me donne le nombre d’items présents dans l’objet.
1 2 3 4 5 6 |
<td> <span style="color: red; font-weight: bold;">{{lineData.details.config | numKeys}}</span> </td> <td ng-repeat="(id, config) in lineData.details.config" class="td-nobor" ng-init="resultCode=config.resultCode;stockMessage=config.stockMessage"> [...] </td> |