问题描述:

How do I write an angular expression that checks each child item in a list and returns true if any child has a certain property value?

I have a chart legend being generated by an ng-repeat expression. Now I want to show the wrapping element only if any of the child items in the list has the property "datavisible" set to true.

Currently I have a $scope variable "anydatavisible" being set to true if any of the data series is activated so I can use that in an expression.

<div class="chart-legend" ng-show="anydatavisible">

<div ng-repeat="item in model.Accounts track by item.Id" ng-if="item.datavisible">

{{item.Name}}

</div>

</div>

I was wondering I can get the same result by using an expression that checks each child so I don't have the need for the variable "$scope.anydatavisible".

I'm looking for something like this:

<div class="chart-legend" ng-show="any(model.Accounts).datavisible">

<div ng-repeat="item in model.Accounts track by item.Id" ng-if="item.datavisible">

{{item.Name}}

</div>

</div>

网友答案:

Try this:

<div class="chart-legend" ng-show="(model.Accounts | filter:{datavisible:true}).length > 0">
相关阅读:
Top