您可以从最新的引导站点查看最低限度的引导轮播:http: //getbootstrap.com/javascript/#carousel
下一步是把它变成一个最小的 AngularJS 指令,事实证明这真的很简单。
链接
由于 Bootstrap 的轮播组件需要 jQuery,因此您需要最新版本的 jQuery:https ://code.jquery.com/jquery-2.1.4.js 。
还从 Bootstrap 添加必要的链接:http: //getbootstrap.com/getting-started/#download
轮播指令
app.directive('carousel', function($timeout) {
return {
restrict: 'E',
scope: {
links: '='
},
templateUrl: 'carousel.html',
link: function(scope, element) {
$timeout(function() {
$('.carousel-indicators li',element).first().addClass('active');
$('.carousel-inner .item',element).first().addClass('active');
});
}
}
});
根据 Bootstrap 的文档:
需要初始活动元素
.active 类需要添加到其中一张幻灯片中。否则,轮播将不可见。
为了在渲染后添加 .active 类,我们$timeout
在链接函数中使用。
轮播.html
这基本上是 Bootstrap 提供的 HTML 的副本,它已被修改ngRepeat
并ngSrc
使其动态化:
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li ng-repeat="link in links" data-target="#carousel-example-generic" data-slide-to="{{$index}}"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item" ng-repeat="link in links">
<img ng-src="{{link.src}}" alt="{{link.src}}">
<div class="carousel-caption">
{{link.caption}}
</div>
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
用法
要使用该指令,请在控制器中初始化链接列表:
app.controller('ctrl', function($scope) {
$scope.links =[
{ src:"http://www.conceptcarz.com/images/Suzuki/suzuki-concept-kizashi-3-2008-01-800.jpg", alt:"", caption:""},
{ src:"http://www.conceptcarz.com/images/Volvo/2009_Volvo_S60_Concept-Image-01-800.jpg", alt:"", caption:""},
{ src:"http://www.sleepzone.ie/uploads/images/PanelImages800x400/TheBurren/General/sleepzone_hostels_burren_800x400_14.jpg", alt:"", caption:""},
];
});
将指令添加到您的视图并传入您之前在控制器中定义的链接模型:
<body ng-controller="ctrl">
<div style="width:800px; height: 400px">
<carousel links="links"></carousel>
</div>
</body>
注意:内联样式是确保图像大小占据整个画布所必需的。可能有更好的方法来做到这一点 - 请参阅 Bootstrap。
演示计划