Stephan K.
Stephan K.

Reputation: 15732

angular.js: Use ng-hide or ng-show to hide last two elements of JSON data

I am using in my HTML:

<div ng-repeat="item in items" ng-show="!$last">
    <div>{{ item.value }}</div>
</div>

..to hide the last element of a mongodb document (aka JSON data). All other elements of the JSON data gets printed out as expected.

Now how can I hide the last two members of my json data, or alternatively hide by key name, e.g. "__v" and "_id" should be hidden from the user.

Upvotes: 1

Views: 2058

Answers (2)

runTarm
runTarm

Reputation: 11547

You could write a custom filter to return all the keys of an object but excluding the ones that you don't want, e.g. keys with _ prefix.

app.filter('keys', function () {
  return function (obj) {
    return Object.keys(obj).filter(function (k) {
      return k && k.charAt(0) !== '_' && k !== '$$hashKey';
    });
  };
});

and use it in ng-repeat like this:

<ul>
  <li ng-repeat="friend in friends">
    <input type="text" ng-repeat="key in friend | keys" ng-model="friend[key]"  />
  </li>
</ul>

Example Plunker: http://plnkr.co/edit/ws5RqR5zN71ZHY7OCxtD?p=preview

Upvotes: 1

bmleite
bmleite

Reputation: 26880

Use the ng-show directive:

<div ng-repeat="friend in friends">
  <li ng-repeat="(key, value) in friend" ng-show="key != '_id' && key != '__v'">
    <input type="text" ng-model="friend[key]">
  </li>
</div>

plunker

Upvotes: 2

Related Questions