AngularJS ng-repeat对象,按对象过滤

AngularJS ng-repeat objects, filtering by objects

本文关键字:对象 过滤 AngularJS ng-repeat      更新时间:2023-09-26

可以迭代对象并检查它们的属性吗?

{
 Obj1 : { visible : true, value : 2}
 Obj2 : { visible : false, value : 4}
 Obj3 : { visible : true, value : 6}
}

.HTML:

<ul ng-show="data.templates.attachments">
        <li ng-repeat="(key, value) in data.templates.attachments | filter :       value.visible">{{key}}</li>
</ul>

如果我只想显示钥匙,它是完美的,但我想隐藏这些隐形。

试试这个

<li ng-repeat="(key, value) in data | filter: {visible: true}">{{value.value}}</li>

更新:

object中的object属性筛选

// only for one property, for example {a: 1}
myApp.filter('filterByProperty', function () {
  return function(items, field) {
      var result = {},
          key    = Object.keys(field).pop(),
          value  = field[key];
      angular.forEach(items, function(el, index) {
          if (el[key] === value) {
              result[index] = el;
          }
      });
      return result;
  };
});

Alexander 已经回答了这个问题,但是如果您仍然想在 dom 中保留每个对象的元素并且只是希望它不被显示(对象中的 visible 属性给人的印象是它可能只是没有显示,而不是不插入到 dom 中),

然后你可以使用ng-classhttp://jsfiddle.net/naeemshaikh27/dKjz5/48/