Solution of infinite digest loop for ng-repeat with object property filter in AngularJS

Few months ago I wrote a code in AngularJS which was something like this:


<ul ng-repeat="course in courses|filter:{is_taken:true }" >
<li>{{ course.name }}</li>
</ul>

 

It was generating a digesting loop error. Though the data was showing correctly. Soon I realized that it was generating because for each turn in ng-repeat loop, courses are filtered up. So, it was watched and digest by default characteristics of angular Scope. In this situation the easiest solution to me was following:


<div ng-init="filteredCourses=courses|filter:{is_taken:true }">
<ul ng-repeat="course infilteredCourses" >
<li>{{ course.name }}</li>
</ul>
</div>

 

What I did was I initiated a new object using ng-init which contains the filtered objects. Then use that newly created object to loop on. And all those errors are gone… 🙂

Like
Like Love Haha Wow Sad Angry