ng repeat repeats more times than array length

84 views
Skip to first unread message

Erivan junior

unread,
Oct 24, 2014, 9:24:18 AM10/24/14
to ang...@googlegroups.com
Hello everybody,

i have a problem and i need your help!

for example i have a object 
$scope.values= [
{group :'a',  value:1 },
{group :'a',  value:2 },
{group :'b',  value:3 },
{group :'a',  value:4 },
]

and i use the ng-if to verify if i show the group

<div>
 ng-repeat=" val in values"
  <div ng-if="show(val.group)">  
     {{val.group}}
   </div>

   <div>  {{val.value}}</div>
 
</div>
and in controller i have function called show

var verifay = '';
$scope.show = function (val) {
  if(verify == val){
         return false;
  }else{
       verifay = val;
        return true;
  }
}

but this not work because de function show is called  more times than array length, anybody can help me?

Sander Elias

unread,
Oct 25, 2014, 1:20:32 AM10/25/14
to ang...@googlegroups.com
Hi Erivan,

This is not a problem with the ngRepeat. Its part of how angular works. You can't do what you are doing
in this way.

the ngIf will gets evaluated on every digest cycle. If there is a change in the models data anywhere, that
will cause a new digest cycle. You can't predict the number of evaluation cycles from within your own code.

If you use a function in a conditional directive (ngIf/ngSwitch/ngShow/ a few others) it must be a function
with no side effects and it needs to return the same result for the same argument every time. 
This is also known as a pure function.

If you build a plunker arround your problem, I can help you at a bit more if you want.

Does this answer your question?

Regards
Sander.


Erivan junior

unread,
Oct 25, 2014, 1:53:07 PM10/25/14
to ang...@googlegroups.com
Thanks Sander,
i use the $index for know when i have to "restart" my function.

more one time thank you!


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/AF3GSlQt4EU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--
Atenciosamente,
Erivan Rodrigues Gonzaga Junior

Erivan junior

unread,
Oct 25, 2014, 2:12:54 PM10/25/14
to ang...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages