我是 angularJS 的新手。我有以下代码:
<div class="col-sm-3 col-xs-6" ng-repeat="image in gallery | limitTo:'4'">
<a href="#"><img ng-src="
<?php echo '...' ?>{{pack.hotels.current.images[$index+1].path}}"
alt="Image" class="img-responsive"></a>
</div>
目前我有一个对象数组,它们有 2 个属性:类型和路径。在 ng-repeat 中,我遍历所有对象。我只想遍历数组中具有属性类型==="GEN">
的对象调试时,我运行:
pack.hotels.current.images.forEach(function(item, count){
if(item.type === "HAB")
console.log(item)
})
如何重写 ng-repeat 语句,使其仅遍历属性类型="GEN">的元素?我应该应用哪种过滤器?谢谢!
像这样使用 ng-repeat:
ng-repeat="image in gallery | filter:{ type: 'GEN'} | limitTo:'4'"
只需添加一个过滤器filter: {type:'GEN'}
.HTML
<div class="col-sm-3 col-xs-6" ng-repeat="image in gallery | filter: {type:'GEN'}| limitTo:'4'">
<a href="#"><img ng-src="
<?php echo '...' ?>{{pack.hotels.current.images[$index+1].path}}"
alt="Image" class="img-responsive"></a>
</div>