dem*_*iak 3 jquery angularjs owl-carousel
我正在尝试做的是使用ng-repeat指令将角度构建到我的轮播结构中,然后将其提供给owl carousel jquery插件.部分技巧是这个旋转木马进入视图(ngView).
遗憾的是,这似乎并不那么简单.
我到目前为止探讨的选项是:
$viewContentLoaded事件.这不起作用,显然不赞成(因为它相当于控制器的DOM操作)基本上我的问题是:有没有其他/更好的方法来做到这一点(与无限循环和HTML字符串连接相反)?
请注意,我需要建立的真正的旋转木马项目比下面的示例更复杂.
好的,现在有些代码:
首先,来自相关视图的HTML片段:
<div class="owl-carousel owl-theme daCarousel" da-carousel="">
</div>
Run Code Online (Sandbox Code Playgroud)
接下来,指令配置:
app.directive('daCarousel',function () {
var makeItLive = function (scope, element, attrs)
{
//TODO feed in real data here
for (var i = 0; i < 10; i++) {
$(element).append ('<div class="item">Item ' + i + '</div>');
}
$(element).owlCarousel({
navigation : true, // Show next and prev buttons
slideSpeed : 300,
paginationSpeed : 400,
singleItem:true
});
};
Run Code Online (Sandbox Code Playgroud)
编辑
好的,同时我发现了这项$templateCache服务.
本质上是在脚本标签中定义一个模板(我的实际上是在相关的视图中):
<script type="text/ng-template" id="specialsTemplate.html">
//HTML with AngularJS bindings etc here
</script>
Run Code Online (Sandbox Code Playgroud)
这样你就可以在你的指令中得到:
var tpl = $templateCache.get('specialsTemplate.html') ;
var compiled = $compile(tpl)(scope);
element.html (compiled);
Run Code Online (Sandbox Code Playgroud)
对于魔法的最后一点,使用oakfish的建议$timeout:
$timeout (
function () {
$('#correctIdForYourCarouselElement').owlCarousel({
navigation : false, // Show next and prev buttons
slideSpeed : 300,
paginationSpeed : 400,
singleItem:true
// "singleItem:true" is a shortcut for:
// items : 1,
// itemsDesktop : false,
// itemsDesktopSmall : false,
// itemsTablet: false,
// itemsMobile : false
});
},
50
);
Run Code Online (Sandbox Code Playgroud)
好的,详细说明我上面的编辑,让我们给出更全面的答案.
首先,您需要一个控制器将数据注入指令的范围(请注意,我在这里不使用隔离范围):
angular.module('ngApp')
.controller('MyCtrl', function ($scope) {
$scope.content = getDataSomeHow();
});
Run Code Online (Sandbox Code Playgroud)
接下来,定义您的模板.我发现相关的观点在我的案例中是个好地方.另请注意,您可能希望了解有关隔离范围的信息.
<script type="text/ng-template" id="specialsTemplate.html">
<div class="item" ng-repeat="group in content" >
<div class="container">
<div class="row" ng-repeat="itemRow in group">
<div class="col-lg-2 outerItemContainer" ng-repeat="item in itemRow">
<div class="itemContainer">
<div class="someClasss">
{{item.description}}
</div>
</div>
</div>
</div>
</div>
</div>
</script>
Run Code Online (Sandbox Code Playgroud)
好的,接下来是实际指令.注意使用pre并post没有任何帮助,我STILL不得不使用该$timeout服务来获得旋转木马的工作.
angular.module('ngApp')
.directive('specialsDirective', function ($compile,$templateCache,$timeout) {
var buildIt = function (scope, element, attrs) {
var tpl = $templateCache.get('specialsTemplate.html') ;
var compiled = $compile(tpl)(scope);
element.html (compiled);
};
var makeItScroll = function (scope, element,attrs) {
/* In THEORY, at this point it should be safe and fine
* to call this jQM plugin. But it is not - does NOT work. Hence
* the kludgy timeout crap here.
*/
$timeout (
function () {
$('#myCarousel').owlCarousel({
navigation : false, // Show next and prev buttons
slideSpeed : 300,
paginationSpeed : 400,
singleItem:true
});
},
50
);
};
return {
restrict: 'EA',
replace:true,
compile: function compile (tElement, tAttrs, transclude){
return {
pre: buildIt,
post: makeItScroll
}
}
};
});
Run Code Online (Sandbox Code Playgroud)
好的,那么最后一点,实际指令使用:
<div ng-controller="MyCtrl" >
<div specials-directive="" id="myCarousel" class="owl-carousel owl-theme">
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4706 次 |
| 最近记录: |