在Angular JS中初始化jQuery插件(RoyalSlider)

时间:2014-05-29 13:36:11

标签: javascript jquery angularjs

尝试将RoyalSlider加载为指令。这是我的指令,虽然我不确定为什么,但在第一次加载时却没有,但在后续加载时没有:

app.directive('royalSlider', ['$timeout', function($timeout) {

    $(".royalSlider").royalSlider({
        keyboardNavEnabled: true,
        arrowsNav: true,
        arrowsNavHideOnTouch: true,
        imageScaleMode: 'fill',
        slidesSpacing: 0
    });

}]);

错误:

TypeError: Cannot read property 'compile' of undefined

假设在所有内容完成后加载问题,我将其更改为:

app.directive('royalSlider', ['$timeout', function($timeout) {
    return {
        link: function ($scope, element, attrs) {
            $scope.$on('$viewContentLoaded', function () {

                $(".royalSlider").royalSlider({
                    keyboardNavEnabled: true,
                    arrowsNav: true,
                    arrowsNavHideOnTouch: true,
                    imageScaleMode: 'fill',
                    slidesSpacing: 0
                });

            })
        }
    }
}]);

没有任何事情发生。 $ timeout也在那里,因为我也试过这个技巧,但没有用。

1 个答案:

答案 0 :(得分:0)

试试这个

app.directive('royalSlider', ['$timeout', function($timeout) {
    return {
        link: function ($scope, element, attrs) {

            $scope.$apply($(".royalSlider").royalSlider({
                    keyboardNavEnabled: true,
                    arrowsNav: true,
                    arrowsNavHideOnTouch: true,
                    imageScaleMode: 'fill',
                    slidesSpacing: 0
                }));


        }
    }
}]);

或者

app.directive('royalSlider', ['$timeout', function($timeout) {
        return {
            link: function ($scope, element, attrs) {

                $(".royalSlider").royalSlider({
                    keyboardNavEnabled: true,
                    arrowsNav: true,
                    arrowsNavHideOnTouch: true,
                    imageScaleMode: 'fill',
                    slidesSpacing: 0
                });
                 $scope.$apply();


            }
        }
    }]);