maria maria - 1 year ago 90
AngularJS Question

angular directory slick execution

I'm using the following plugin:

I'm also using ng-repeat, so I'm experiencing that it's executing before the ng-repeat is done.

Is there any way to fix that?

<slick><div ng-repeat="carfactory in vm.carfactory">
<h4> {{ }}</h4>
<img src="/biler/1.png" style="float: left; padding: 15px; max-width: 100px; height: auto; margin-bottom: 26px;">

<p><b>Pris:</b> {{ carfactory.price | currency }} kroner</p>
<p><b>Eier:</b> <a href="/">{{ carfactory.ownerid }}</a></p>

<b>Beskyttelse:</b> {{ }}</p><p><b>Antall igjen:</b> <span id="antall_1">{{ carfactory.amount }}</span>

<p class="hotel-out">
<input ng-show="carfactory.amount > 0" class="btn btn-block" ng-click="vm.purchasecar(carfactory)" type="submit"value="Kjøp bil!">
<p class="hotel-in alert alert-warning" ng-show="carfactory.amount <= 0">Gå ut av hotell for å kjøpe en bil.</p>

That doesn't work, but if I remove the repeater and copy+paste the code two times it works.

Any idea how to make the ng-repeat work, or how I can get the slicker plugin to wait until it is finished loading?


if i perform
<slick init-onload="true" data="vm.carfactory"
then i get the following error:

slider.unslick is not a function

edit: my controller is using angular.module('garage',['slick'])

Answer Source

My assumption is that slick is called before the items in ng-repeat are rendered. You can try (re)executing the slick() function when the ng-repeat finishes rendering using the following code sample:

<div ng-repeat="carfactory in vm.carfactory" ng-init="$last && reloadSlick()">

$last variable ensures that the function is called only for the last item (one time).

Just as a safety measure you can wrap the code in reloadSlick() with setTimeout() so its execution waits for the next event loop.

$scope.reloadSlick = function() {
  setTimeout(function() {... code here ...})

More references about ng-repeat rendering callback:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download