简体   繁体   中英

How to set scope variable inside ng-repeat?

I want to determine whether any ng-repeated element is rendered.

I write this code

<div ng-show="anyRendered">Any has been rendered</div>

 <div ng-show="anyFilterActive">ANY FILTER IS ACTIVE</div>

 <div class="selected-filter-value" ng-if="filter.name && filterCtrl.isActiveFilter(filter)" data-ng-repeat="(name, filter) in filterOptions">
    <span ng-init="anyFilterActive = true;">{{filter.name}}:</span>

</div>

But this code doesn't work. Also I try to write $parent.anyRendered inside ng-repeat.

Try with ng-if

<div ng-show="filterOptions.length > 0">Any has been rendered</div>

<div ng-if="filterOptions.length > 0" ng-repeat="(name, filter) in filterOptions">
<span></span>
</div>

You must not write code logic in your templates: all the code must be in your controllers, filters, directives and services. The ng-init directive is only here so that you can create aliases to make your templates simpler.

Filters and directives contain "presentation code" and controllers and services contain "business related code".

You could create a filter to know if an object is empty or not ( filter code from another question ), and use it with ng-if, ng-show...

Resulting template:

<div ng-if="filterOptions|empty">
     Nothing to show! 
</div>
<div ng-repeat="(k, v) in filterOptions">
     {{k}}, {{v}}
</div>

just use $index

<div ng-repeat="(name, filter) in filterOptions">

<span ng-show="$index > 0"></span>

</div>

ngRepeat Docs $index

In the example in the ng-repeat docs u can see the usage of $index

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM