Serge Serge - 5 months ago 62
AngularJS Question

Filter in angular ng-init

I try to initialise a collection in the

ng-init
. Why this code does not work?

<div ng-app="myApp" ng-controller="PeopleCtrl">
<ul ng-init="myPeople=(people|filter:{show:true})" ng-repeat="person in myPeople">
<li>{{person.name}}, {{person.show}}
</ul>
</div>


CodePen

I don't need using
ng-if
or other things, I just wonder if possible to initialise a collection using a filter in
ng-init

Answer

ng-init would not work when data is coming asynchronously, and I don't think so ng-init suits your case.

I'd suggest you to use alias for filtered data in ng-repeat itself, while filtering myPeople collection

ng-repeat="person in people|filter:{show:true} as myPeople"