I'm building an app in AngularJS. My app also uses a third party javascript library that acts on a div like this one:
<div id="turn-me-into-a-slider"></div>
Now I actually have several of these divs, and use an ng-repeat to make them. So it really looks more like this:
<div id="sliders-container" ng-repeat="slider in sliders track by $index">
<div id="slider-{{$index}}"></div>
</div>
Now in my code, when I want to add a slider first I add a new slider to the sliders
array. That should propagate into the DOM and then I can run the initialization function for the slider on the div with id="slider-n"
. This is where the problem comes in. If I put the initialization function for the slider right after pushing a new value onto the sliders
array, the DOM hasn't updated yet, and when the slider initializer is called, there is no DOM element with the proper id yet, and initialization fails. If I call $scope.$apply()
first, that fixes the problem, but occasionally I get a error, $scope.$apply() already in progress
. Is there a way I can call $scope.$apply() only when it's not currently running to avoid this? If you have any other suggestions, I'd welcome them.