Jquery bxslider not working + Angular js ng-repeat issue

会有一股神秘感。 提交于 2019-12-04 16:56:21

bxSlider doesn't work because the jQuery executes before your ng-repeat has finished.

You can use something like finishingmoves' directive to execute a function after ng-repeat has finished.

The directive is the following:

var module = angular.module('testApp', [])
.directive('onFinishRender', function ($timeout) {
return {
    restrict: 'A',
    link: function (scope, element, attr) {
        if (scope.$last === true) {
            $timeout(function () {
                scope.$emit('ngRepeatFinished');
            });
        }
    }
    }
});

(finishingmoves directive from here: Calling a function when ng-repeat has finished)

You can see an example of it here: http://jsfiddle.net/paulocoelho/BsMqq/4/

Hope that helps and don't forget to vote!

sandeep kumar

You can call method when window load like this use

$(window).bind("load", function() {  
 });

in the place of

$(document).ready(function(){
});

Not my solution, but I'd thought I would pass it along.

I like this solution the best (Utilizes directive controllers)

// slightly modified from jsfiddle

// bxSlider directive
controller: function() {},
link: function (scope, element, attrs, controller) {
    controller.initialize = function() {
        element.bxSlider(BX_SLIDER_OPTIONS);
    };
}

// bxSliderItem directive
require: '^bxSlider',
link: function(scope, element, attrs, controller) {
    if (scope.$last) {
        controller.initialize();
    }
}

http://jsfiddle.net/CaioToOn/Q5AcH/8/

In ng-repeat call any function(ex:initBxslider()) on $last object

<div ng-repeat="term in terms" >  
  {{term.name}}
  <span ng-init="$last && data.initBxslider()"></span>
</div> 

in controller:

 $scope.initBxslider = function () {
        $timeout(function () {
            $('.slider4').bxSlider({
               slideWidth: 300,
               minSlides: 2,
               maxSlides: 3,
               moveSlides: 1,
               slideMargin: 10
             });
        });
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!