如何在猫头鹰旋转木马的角度制作指令?

你可以告诉我如何在角度js中制作dirctive。我需要在js中使用owl carousel插件,就像我在jqm小提琴中所做的那样http://jsfiddle.net/ezanker/o9foej5L/1/ 。我需要做同样的事情你可以告诉我如何实现这个使用指令http://plnkr.co/edit/4ySYwsqrBKUJUj6MwoRY?p=catalogue

              

one

two

three

four

你可以使用这样的指令:

 app.directive('owlCarousel', function() { return { restrict: 'A', scope: { owlOptions: '=' }, link: function(scope, element, attrs) { $(element).owlCarousel(scope.owlOptions); } }; }); 

在HTML上添加它作为属性:

 
...

演示

这是我的一般解决方案。 这适用于ngrepeat和范围的刷新数据。 诀窍是为最后一项添加一个指令,因此当最后一项在dom中时会触发init。

 
...

…… js

 .directive('owlCarousel', ['$timeout', function ($timeout) { return { restrict: 'A', link: function (scope, element, attrs) { scope.initCarousel = function () { // hacky ondomready $timeout(function hackyDomReady() { // if is already initialized destroy and re create it // $(element).data().owlCarousel <- owl 2 if ($(element).data('owlCarousel')) { // <- owl 1 // $(element).trigger('destroy.owl.carousel'); // <- owl 2 $(element).data('owlCarousel').destroy();// <- owl 1 } $(element).owlCarousel({ autoPlay: 10000, navigation: true, items: attrs.owlSlidesCount }); }); }; } }; }]) .directive('owlCarouselItem', [function () { return { restrict: 'A', transclude: false, link: function (scope, element) { if (scope.$last) { scope.initCarousel(); } } }; }])