Owl Carousel没有识别ng-repeat的元素
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Owl Carousel没有识别ng-repeat的元素相关的知识,希望对你有一定的参考价值。
我想让 owl carousel在Angular工作。
我希望在我的视图中标记这样的标记,因为我有很多画廊:
<owl-carousel owl-options="owlOptions">
<div ng-repeat="image in gallery" class="item">
<p>hello</p>
</div>
</owl-carousel>
基本上所有指令应该做的是转盘的初始化。该指令完美无缺,除非我使用ng-repeat。我猜这个指令是在处理ng-repeat之前加载的。
有没有人对如何解决这个问题有任何想法,而不为每种类型的滑块构建模板和指令?
非常感谢!
这是指令:
angular.module('dir.owlCarousel', [])
.directive('owlCarousel',[function() {
return {
restrict: 'EA',
transclude: false,
scope: {
owlOptions: '='
},
link: function(scope, element, attrs) {
$(element).owlCarousel(scope.owlOptions);
}
};
}]);
答案
你想看看这些答案:
AngularJS event for when model binding or ng-repeat is complete?
angular.module('dir.owlCarousel', [])
.directive('owlCarousel',[function() {
return {
restrict: 'EA',
transclude: false,
scope: {
owlOptions: '='
},
link: function(scope, element, attrs) {
scope.initCarousel = function() {
$(element).owlCarousel(scope.owlOptions);
};
}
}
};
}])
.directive('owlCarouselItem',[function() {
return function(scope) {
if (scope.$last) {
scope.initCarousel();
}
};
}]);
<owl-carousel owl-options="owlOptions">
<div owl-carousel-item ng-repeat="image in gallery" class="item">
<p>hello</p>
</div>
</owl-carousel>
另一答案
必须对您的指令进行一些更改,以便它可以在同一页面上使用多个轮播。这是一个工作plnkr的链接
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.items1 = [1,2,3,4,5];
$scope.items2 = [1,2,3,4,5,6,7,8,9,10];
}).directive("owlCarousel", function() {
return {
restrict: 'E',
transclude: false,
link: function (scope) {
scope.initCarousel = function(element) {
// provide any default options you want
var defaultOptions = {
};
var customOptions = scope.$eval($(element).attr('data-options'));
// combine the two options objects
for(var key in customOptions) {
defaultOptions[key] = customOptions[key];
}
// init carousel
$(element).owlCarousel(defaultOptions);
};
}
};
})
.directive('owlCarouselItem', [function() {
return {
restrict: 'A',
transclude: false,
link: function(scope, element) {
// wait for the last item in the ng-repeat then call init
if(scope.$last) {
scope.initCarousel(element.parent());
}
}
};
}]);
这是html
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.theme.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.transitions.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.js" />
<script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.15/angular.js" data-semver="1.3.15"></script>
<script data-require="jquery@2.1.3" data-semver="2.1.3" src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<data-owl-carousel class="owl-carousel" data-options="{navigation: true, pagination: false, rewindNav : false}">
<div owl-carousel-item="" ng-repeat="item in ::items1" class="item">
<p>{{::item}}</p>
</div>
</data-owl-carousel>
<data-owl-carousel class="owl-carousel" data-options="{navigation: false, pagination: true, rewindNav : false}">
<div owl-carousel-item="" ng-repeat="item in ::items2" class="item">
<p>{{::item}}</p>
</div>
</data-owl-carousel>
</body>
</html>
以上是关于Owl Carousel没有识别ng-repeat的元素的主要内容,如果未能解决你的问题,请参考以下文章
有没有办法改变owl-carousel的光标? (不是 owl-dots 或 owl-nav)