Angular ng-repeat is causing multiple loops
up vote
0
down vote
favorite
My test data set has one element in the array. I am using an ng-repeat to loop through the array to display the element. The display HTML calls a function per rendered element - in this case, it should only be called once because there is only one element in the array. However, even though only one element is being displayed properly, I am seeing the function being called 3 times even though it should only be called once.
The element is clickable, revealing sub-info of that main element. When the element is clicked, the same function is called a 4th time.
My data set, output from a console.log(clubs)
in the controller:
{success: true, data: Array(1)}
data: Array(1)
0: {clID: "1", cID: "1001", clTime: "19:29:15", clDate: "2018-08-27", clActive: "1", …}
length: 1
__proto__: Array(0)
success: true
__proto__: Object
The HTML from my template. The {{ showClub() }}
is a test function simply to show how many times the function is being called and is displaying console.log output. In this case, the ng-repeat is calling it 3 times...and should only be calling it once. Note there is a.:
<div id="club_tabItem_{{club.cID}}" style="padding:5px;border-bottom:1px solid grey;" ng-click="showEvents(club.clID);" class="item-remove-animate item-avatar" ng-repeat="club in clubs.data">
<div id="loc_{{club.clID}}" style="left:15px;">
<h3>{{club.clVendor}}
{{ showClub() }} <!-- this is the test function -->
<!-- <span style="margin-left:15px;">Location: {{club.data[0].clAddress1}}, {{club.data[0].clCity}}</span> -->
<i style="font-size:25px;float:right;" class="icon ion-android-arrow-dropdown-circle icon-accessory customColor1"></i>
</h3>
<span style="padding-left:30px;">{{club.clDesc}}</span>
<div id="eventsDiv_{{club.clID}}" style="width:100%;display:none;margin-top:10px;background-color:lightGrey;"
ng-if="club.cID == event.cID"
ng-click="$location.url('/tab/clubs/{{event.ceID}}')"
ng-repeat="event in club.events"
>
<div id="events_{{event.ceID}}" style="width:80%;margin-left:20%;display:inline-block;" >
<div style="float:left;font-size:14px;font-weight:bolder;">{{event.ceName}} ({{event.ceName1}}) </div>
<div style="float:left;margin-left:15px;">Location: {{club.clAddress1}}, {{club.clCity}}</div>
<div style="float:left;margin-left:15px;">Next: {{ nextEvent(event) }}</div>
</div>
</div>
</div>
</div>
The above rendered HTML lists the one element...when its clicked on, to unhide the sub-elements, the same {{ showClub() }}
is called again for a 4th time.
Init Club: 0
Init Club: 1
Init Club: 2
<!-- above three outputs are from initial ng-repeat, below is from clicking on the rendered element, triggering the same function call again -->
Init Club: 3
In my controller:
clubService.all()
.then(function(response) {
$scope.clubs = response ;
console.log($scope.clubs) ; //output above
}) ;
$scope.clubCount = 0 ;
$scope.showClub = function() {
console.log("Init Club: " +$scope.clubCount) ;
$scope.clubCount++ ;
}
My biggest concern is that when I move to real data...that will list 10 items on average, that instead of 10 separate calls its going to be making a total of 40.
What is causing the ng-repeat to do this? (assuming this is the issue).
javascript angularjs ionic-framework angularjs-ng-repeat
add a comment |
up vote
0
down vote
favorite
My test data set has one element in the array. I am using an ng-repeat to loop through the array to display the element. The display HTML calls a function per rendered element - in this case, it should only be called once because there is only one element in the array. However, even though only one element is being displayed properly, I am seeing the function being called 3 times even though it should only be called once.
The element is clickable, revealing sub-info of that main element. When the element is clicked, the same function is called a 4th time.
My data set, output from a console.log(clubs)
in the controller:
{success: true, data: Array(1)}
data: Array(1)
0: {clID: "1", cID: "1001", clTime: "19:29:15", clDate: "2018-08-27", clActive: "1", …}
length: 1
__proto__: Array(0)
success: true
__proto__: Object
The HTML from my template. The {{ showClub() }}
is a test function simply to show how many times the function is being called and is displaying console.log output. In this case, the ng-repeat is calling it 3 times...and should only be calling it once. Note there is a.:
<div id="club_tabItem_{{club.cID}}" style="padding:5px;border-bottom:1px solid grey;" ng-click="showEvents(club.clID);" class="item-remove-animate item-avatar" ng-repeat="club in clubs.data">
<div id="loc_{{club.clID}}" style="left:15px;">
<h3>{{club.clVendor}}
{{ showClub() }} <!-- this is the test function -->
<!-- <span style="margin-left:15px;">Location: {{club.data[0].clAddress1}}, {{club.data[0].clCity}}</span> -->
<i style="font-size:25px;float:right;" class="icon ion-android-arrow-dropdown-circle icon-accessory customColor1"></i>
</h3>
<span style="padding-left:30px;">{{club.clDesc}}</span>
<div id="eventsDiv_{{club.clID}}" style="width:100%;display:none;margin-top:10px;background-color:lightGrey;"
ng-if="club.cID == event.cID"
ng-click="$location.url('/tab/clubs/{{event.ceID}}')"
ng-repeat="event in club.events"
>
<div id="events_{{event.ceID}}" style="width:80%;margin-left:20%;display:inline-block;" >
<div style="float:left;font-size:14px;font-weight:bolder;">{{event.ceName}} ({{event.ceName1}}) </div>
<div style="float:left;margin-left:15px;">Location: {{club.clAddress1}}, {{club.clCity}}</div>
<div style="float:left;margin-left:15px;">Next: {{ nextEvent(event) }}</div>
</div>
</div>
</div>
</div>
The above rendered HTML lists the one element...when its clicked on, to unhide the sub-elements, the same {{ showClub() }}
is called again for a 4th time.
Init Club: 0
Init Club: 1
Init Club: 2
<!-- above three outputs are from initial ng-repeat, below is from clicking on the rendered element, triggering the same function call again -->
Init Club: 3
In my controller:
clubService.all()
.then(function(response) {
$scope.clubs = response ;
console.log($scope.clubs) ; //output above
}) ;
$scope.clubCount = 0 ;
$scope.showClub = function() {
console.log("Init Club: " +$scope.clubCount) ;
$scope.clubCount++ ;
}
My biggest concern is that when I move to real data...that will list 10 items on average, that instead of 10 separate calls its going to be making a total of 40.
What is causing the ng-repeat to do this? (assuming this is the issue).
javascript angularjs ionic-framework angularjs-ng-repeat
2
You are seeing an effect of how the$digest
cycle in angular works here.ng-repeat
isn't a strict loop that only fires one time. Instead, it is re-evaluated every time that a new element must be added or changed in the DOM. and since you have an innerng-repeat
that would also be causing the DOM to be re-evaluated..... This is a prime example of why putting function calls in the DOM render loop is a bad idea.
– Claies
Nov 19 at 17:47
@Claies - oy! I never new that. I thought it was a strict loop. My html renders properly but seeing this function repeat itself made me concerned. I will try to figure out another way of getting the data from the function call before the html is rendered. Been scratching my head on this one for a while.
– rolinger
Nov 19 at 18:11
Possible duplicate of Using function with ng-repeat causing infinite digest loop
– nkuma_12
Nov 20 at 21:21
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
My test data set has one element in the array. I am using an ng-repeat to loop through the array to display the element. The display HTML calls a function per rendered element - in this case, it should only be called once because there is only one element in the array. However, even though only one element is being displayed properly, I am seeing the function being called 3 times even though it should only be called once.
The element is clickable, revealing sub-info of that main element. When the element is clicked, the same function is called a 4th time.
My data set, output from a console.log(clubs)
in the controller:
{success: true, data: Array(1)}
data: Array(1)
0: {clID: "1", cID: "1001", clTime: "19:29:15", clDate: "2018-08-27", clActive: "1", …}
length: 1
__proto__: Array(0)
success: true
__proto__: Object
The HTML from my template. The {{ showClub() }}
is a test function simply to show how many times the function is being called and is displaying console.log output. In this case, the ng-repeat is calling it 3 times...and should only be calling it once. Note there is a.:
<div id="club_tabItem_{{club.cID}}" style="padding:5px;border-bottom:1px solid grey;" ng-click="showEvents(club.clID);" class="item-remove-animate item-avatar" ng-repeat="club in clubs.data">
<div id="loc_{{club.clID}}" style="left:15px;">
<h3>{{club.clVendor}}
{{ showClub() }} <!-- this is the test function -->
<!-- <span style="margin-left:15px;">Location: {{club.data[0].clAddress1}}, {{club.data[0].clCity}}</span> -->
<i style="font-size:25px;float:right;" class="icon ion-android-arrow-dropdown-circle icon-accessory customColor1"></i>
</h3>
<span style="padding-left:30px;">{{club.clDesc}}</span>
<div id="eventsDiv_{{club.clID}}" style="width:100%;display:none;margin-top:10px;background-color:lightGrey;"
ng-if="club.cID == event.cID"
ng-click="$location.url('/tab/clubs/{{event.ceID}}')"
ng-repeat="event in club.events"
>
<div id="events_{{event.ceID}}" style="width:80%;margin-left:20%;display:inline-block;" >
<div style="float:left;font-size:14px;font-weight:bolder;">{{event.ceName}} ({{event.ceName1}}) </div>
<div style="float:left;margin-left:15px;">Location: {{club.clAddress1}}, {{club.clCity}}</div>
<div style="float:left;margin-left:15px;">Next: {{ nextEvent(event) }}</div>
</div>
</div>
</div>
</div>
The above rendered HTML lists the one element...when its clicked on, to unhide the sub-elements, the same {{ showClub() }}
is called again for a 4th time.
Init Club: 0
Init Club: 1
Init Club: 2
<!-- above three outputs are from initial ng-repeat, below is from clicking on the rendered element, triggering the same function call again -->
Init Club: 3
In my controller:
clubService.all()
.then(function(response) {
$scope.clubs = response ;
console.log($scope.clubs) ; //output above
}) ;
$scope.clubCount = 0 ;
$scope.showClub = function() {
console.log("Init Club: " +$scope.clubCount) ;
$scope.clubCount++ ;
}
My biggest concern is that when I move to real data...that will list 10 items on average, that instead of 10 separate calls its going to be making a total of 40.
What is causing the ng-repeat to do this? (assuming this is the issue).
javascript angularjs ionic-framework angularjs-ng-repeat
My test data set has one element in the array. I am using an ng-repeat to loop through the array to display the element. The display HTML calls a function per rendered element - in this case, it should only be called once because there is only one element in the array. However, even though only one element is being displayed properly, I am seeing the function being called 3 times even though it should only be called once.
The element is clickable, revealing sub-info of that main element. When the element is clicked, the same function is called a 4th time.
My data set, output from a console.log(clubs)
in the controller:
{success: true, data: Array(1)}
data: Array(1)
0: {clID: "1", cID: "1001", clTime: "19:29:15", clDate: "2018-08-27", clActive: "1", …}
length: 1
__proto__: Array(0)
success: true
__proto__: Object
The HTML from my template. The {{ showClub() }}
is a test function simply to show how many times the function is being called and is displaying console.log output. In this case, the ng-repeat is calling it 3 times...and should only be calling it once. Note there is a.:
<div id="club_tabItem_{{club.cID}}" style="padding:5px;border-bottom:1px solid grey;" ng-click="showEvents(club.clID);" class="item-remove-animate item-avatar" ng-repeat="club in clubs.data">
<div id="loc_{{club.clID}}" style="left:15px;">
<h3>{{club.clVendor}}
{{ showClub() }} <!-- this is the test function -->
<!-- <span style="margin-left:15px;">Location: {{club.data[0].clAddress1}}, {{club.data[0].clCity}}</span> -->
<i style="font-size:25px;float:right;" class="icon ion-android-arrow-dropdown-circle icon-accessory customColor1"></i>
</h3>
<span style="padding-left:30px;">{{club.clDesc}}</span>
<div id="eventsDiv_{{club.clID}}" style="width:100%;display:none;margin-top:10px;background-color:lightGrey;"
ng-if="club.cID == event.cID"
ng-click="$location.url('/tab/clubs/{{event.ceID}}')"
ng-repeat="event in club.events"
>
<div id="events_{{event.ceID}}" style="width:80%;margin-left:20%;display:inline-block;" >
<div style="float:left;font-size:14px;font-weight:bolder;">{{event.ceName}} ({{event.ceName1}}) </div>
<div style="float:left;margin-left:15px;">Location: {{club.clAddress1}}, {{club.clCity}}</div>
<div style="float:left;margin-left:15px;">Next: {{ nextEvent(event) }}</div>
</div>
</div>
</div>
</div>
The above rendered HTML lists the one element...when its clicked on, to unhide the sub-elements, the same {{ showClub() }}
is called again for a 4th time.
Init Club: 0
Init Club: 1
Init Club: 2
<!-- above three outputs are from initial ng-repeat, below is from clicking on the rendered element, triggering the same function call again -->
Init Club: 3
In my controller:
clubService.all()
.then(function(response) {
$scope.clubs = response ;
console.log($scope.clubs) ; //output above
}) ;
$scope.clubCount = 0 ;
$scope.showClub = function() {
console.log("Init Club: " +$scope.clubCount) ;
$scope.clubCount++ ;
}
My biggest concern is that when I move to real data...that will list 10 items on average, that instead of 10 separate calls its going to be making a total of 40.
What is causing the ng-repeat to do this? (assuming this is the issue).
javascript angularjs ionic-framework angularjs-ng-repeat
javascript angularjs ionic-framework angularjs-ng-repeat
edited Nov 19 at 17:35
asked Nov 19 at 17:21
rolinger
9111423
9111423
2
You are seeing an effect of how the$digest
cycle in angular works here.ng-repeat
isn't a strict loop that only fires one time. Instead, it is re-evaluated every time that a new element must be added or changed in the DOM. and since you have an innerng-repeat
that would also be causing the DOM to be re-evaluated..... This is a prime example of why putting function calls in the DOM render loop is a bad idea.
– Claies
Nov 19 at 17:47
@Claies - oy! I never new that. I thought it was a strict loop. My html renders properly but seeing this function repeat itself made me concerned. I will try to figure out another way of getting the data from the function call before the html is rendered. Been scratching my head on this one for a while.
– rolinger
Nov 19 at 18:11
Possible duplicate of Using function with ng-repeat causing infinite digest loop
– nkuma_12
Nov 20 at 21:21
add a comment |
2
You are seeing an effect of how the$digest
cycle in angular works here.ng-repeat
isn't a strict loop that only fires one time. Instead, it is re-evaluated every time that a new element must be added or changed in the DOM. and since you have an innerng-repeat
that would also be causing the DOM to be re-evaluated..... This is a prime example of why putting function calls in the DOM render loop is a bad idea.
– Claies
Nov 19 at 17:47
@Claies - oy! I never new that. I thought it was a strict loop. My html renders properly but seeing this function repeat itself made me concerned. I will try to figure out another way of getting the data from the function call before the html is rendered. Been scratching my head on this one for a while.
– rolinger
Nov 19 at 18:11
Possible duplicate of Using function with ng-repeat causing infinite digest loop
– nkuma_12
Nov 20 at 21:21
2
2
You are seeing an effect of how the
$digest
cycle in angular works here. ng-repeat
isn't a strict loop that only fires one time. Instead, it is re-evaluated every time that a new element must be added or changed in the DOM. and since you have an inner ng-repeat
that would also be causing the DOM to be re-evaluated..... This is a prime example of why putting function calls in the DOM render loop is a bad idea.– Claies
Nov 19 at 17:47
You are seeing an effect of how the
$digest
cycle in angular works here. ng-repeat
isn't a strict loop that only fires one time. Instead, it is re-evaluated every time that a new element must be added or changed in the DOM. and since you have an inner ng-repeat
that would also be causing the DOM to be re-evaluated..... This is a prime example of why putting function calls in the DOM render loop is a bad idea.– Claies
Nov 19 at 17:47
@Claies - oy! I never new that. I thought it was a strict loop. My html renders properly but seeing this function repeat itself made me concerned. I will try to figure out another way of getting the data from the function call before the html is rendered. Been scratching my head on this one for a while.
– rolinger
Nov 19 at 18:11
@Claies - oy! I never new that. I thought it was a strict loop. My html renders properly but seeing this function repeat itself made me concerned. I will try to figure out another way of getting the data from the function call before the html is rendered. Been scratching my head on this one for a while.
– rolinger
Nov 19 at 18:11
Possible duplicate of Using function with ng-repeat causing infinite digest loop
– nkuma_12
Nov 20 at 21:21
Possible duplicate of Using function with ng-repeat causing infinite digest loop
– nkuma_12
Nov 20 at 21:21
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379719%2fangular-ng-repeat-is-causing-multiple-loops%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You are seeing an effect of how the
$digest
cycle in angular works here.ng-repeat
isn't a strict loop that only fires one time. Instead, it is re-evaluated every time that a new element must be added or changed in the DOM. and since you have an innerng-repeat
that would also be causing the DOM to be re-evaluated..... This is a prime example of why putting function calls in the DOM render loop is a bad idea.– Claies
Nov 19 at 17:47
@Claies - oy! I never new that. I thought it was a strict loop. My html renders properly but seeing this function repeat itself made me concerned. I will try to figure out another way of getting the data from the function call before the html is rendered. Been scratching my head on this one for a while.
– rolinger
Nov 19 at 18:11
Possible duplicate of Using function with ng-repeat causing infinite digest loop
– nkuma_12
Nov 20 at 21:21