angular directory slick execution

2020-07-18 20:11发布

问题:

I'm using the following plugin: http://vasyabigi.github.io/angular-slick/

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> {{ carfactory.name }}</h4>
        <p>
            <img src="/biler/1.png" style="float: left; padding: 15px; max-width: 100px; height: auto; margin-bottom: 26px;">
        </p>

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

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

        <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>
        </p>
</div></slick>

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?

edit:

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'])

回答1:

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:

  • Calling a function when ng-repeat has finished
  • ng-repeat finish event