angular directory slick execution

一笑奈何 提交于 2019-12-21 20:44:14

问题


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


来源:https://stackoverflow.com/questions/39299307/angular-directory-slick-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!