尝试加载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也在那里,因为我也尝试过这种技巧,但无济于事。
最佳答案
尝试这个
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();
}
}
}]);
关于javascript - 在Angular JS中初始化jQuery插件(RoyalSlider),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23934698/