I am having some weird issue with ng-class
, and I am suspecting that it has to do with race condition.
Here is plunker example
Here is the relevant js code
self.slideLeft = function() {
if (self.end_index < self.list_of_stuff.length) {
self.direction = 'left';
debugger;
self.start_index = self.start_index + 4;
self.end_index = self.end_index + 4;
self.display_list = self.list_of_stuff.slice(self.start_index, self.end_index);
}
}
self.slideRight = function() {
if (self.start_index > 0) {
self.direction = 'right';
debugger;
self.start_index = self.start_index - 4;
self.end_index = self.end_index - 4;
self.display_list = self.list_of_stuff.slice(self.start_index, self.end_index);
}
}
Here is the relevant html
<div class="stuff-wrapper">
<div class="stuff"
ng-class="bCtrl.directionClass()"
ng-repeat="stuff in bCtrl.display_list">
{{stuff}}
</div>
</div>
Here is the animation.
.left.ng-enter,
.left.ng-leave {
-webkit-transition: all 1s ease-in-out;
}
.left.ng-enter {
transition-delay: 0.7s;
opacity: 0;
left: 10%;
}
.left.ng-enter-active {
opacity: 1;
left: 0;
}
.left.ng-leave {
opacity: 1;
left: -10%;
}
.left.ng-leave-active {
left: -20%;
opacity: 0;
}
This is a simple app that slide a list of number left and right.
If the left button is pressed, the numbers slide left.
If the right button is pressed, the numbers slide right.
But we see that if there are a change of direction, the number will first slide in the wrong direction, and the subsequent direction will be correct.
I suspected that this is due to race condition.
Indeed, I see that ng-class
does not get applied right after I changed the direction self.direction
using the debugger.
This is very curious.
Is there a way to combat this?