详解如何将angular-ui的图片轮播组件封装成一个指令
在项目开发中我们经常会遇到图片轮播的功能点:
如果我们开发人员自己原生手写,将会花费很多的时间,最终得不偿失。
接下来就详细说说如何使用angular-ui发热图片轮播模块,并且将它写成一个指令(便于复用)
一如既往的我们项目中使用时requireJS进行js代码的编译
准备工作:
1):引入angularJS , ui-bootstrap-tpls-1.3.2(我使用的是1.3.2版本的)
第一步:自己写一个指令(命名为picchange)
说明:指令控制器中的代码都是angualr-ui官网上拷贝的(因为此文章的重点是如何将其封装成指令,其他的不做重点)
指令的js代码
define(['app'],function(myapp){
myapp.directive('picchange',[function(){
return {
scope:{
picurl:'=',
},
controller:['$scope',function($scope){
$scope.myInterval = 5000;//轮播的时间间隔
$scope.noWrapSlides = false;//是否循环轮播
$scope.active = 0;//起始所显示的图片(0:下标为0的图片)
var slides = $scope.slides = [];//用于存放图片地址
var currIndex = 0;
$scope.addSlide = function() {
var newWidth = slides.length + 1;
slides.push({
image: $scope.picurl[newWidth].imgUrl,//图片的url
text: $scope.picurl[newWidth].wordDes,//图片的描述文字
id: currIndex++
});
};
//................随机...........
$scope.randomize = function() {
var indexes = generateIndexesArray();
assignNewIndexesToSlides(indexes);
};
for (var i = 0;i<$scope.picurl.length;i++) {
$scope.addSlide();
}
// Randomize logic below
function assignNewIndexesToSlides(indexes) {
for (var i = 0, l = slides.length; i < l; i++) {
slides[i].id = indexes.pop();
}
}
function generateIndexesArray() {
var indexes = [];
for (var i = 0; i < currIndex; ++i) {
indexes[i] = i;
}
return shuffle(indexes);
}
// http://stackoverflow.com/questions/962802#962890
function shuffle(array) {
var tmp, current, top = array.length;
if (top) {
while (--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
}
return array;
}
}],
templateUrl:'js/directives/picchange/picchange.html',//轮播的页面
link:function(s,e,attrs){
},
}
}]);
});
好了上面的代码都是拷贝来的,不做解释
轮播模块的html:(picchange.html),指令的html(这个没啥理解的)
指令的html
<div>
<div style="height: 305px;">
<uib-carousel no-wrap="noWrapSlides" interval="myInterval" active="active">
<uib-slide index="slide.id" ng-repeat="slide in slides track by slide.id">
<img style="margin: auto;" ng-src="{{slide.image}}">
<div class="carousel-caption">
<h4>Slide {{slide.id}}</h4>
<p>{{slide.text}}</p>
</div>
</uib-slide>
</uib-carousel>
</div>
<div class="row">
<div class="col-md-6">
<button class="btn btn-info" type="button" ng-click="addSlide()">Add Slide</button>
<button class="btn btn-info" type="button" ng-click="randomize()">Randomize slides</button>
<div class="checkbox">
<label>
<input type="checkbox" ng-model="noWrapSlides">
Disable Slide Looping
</label>
</div>
</div>
<div class="col-md-6">
Interval, in milliseconds: <input class="form-control" type="number" ng-model="myInterval">
<br>Enter a negative number or 0 to stop the interval.
</div>
</div>
</div>
到此为止关于指令的封装已经完成,接下来是如何使用的问题:
(1)有一个页面要用到此指令:(命名为test.html)
<p>图片的轮播</p> <div picurl="img" picchange=""></div><!--img是用来传递参数的-->
test.html对应的控制器:(idea_test_ctrl)
define(['app','directives/picchange/picchange'],function(myapp){
myapp.controller('idea_test_ctrl',['$scope',function($scope){
console.log("this is idea_test_ctrl 的控制器");
$scope.img=[//img是一个对象,其中包含了图片的地址,以及文字描述
{imgUrl:'images/test/1.jpg',wordDes:'this is good pic'},
{imgUrl:'images/test/2.jpg',wordDes:'这是一张很好看的图片'},
{imgUrl:'images/test/3.jpg',wordDes:'it is good pic'}
];
}]);
});
这里给出我的路由配置,便于大家理解:
.state('home.ideas.test', {//(测试)
url: '/test',
views: {
"part": {
templateUrl: 'tpls/ideas/test.html',
controller:"idea_test_ctrl"
}
}
})
到此已经讲解完;
ui-bootstrap的地址:http://angular-ui.github.io/bootstrap/versioned-docs/1.3.2/
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
详解Angular 中 ngOnInit 和 constructor 使用场景
最初学习Angular的时候总是搞不清楚ngOnInit和constructor的区别,现在我们来稍微理一下两者之间的区别。2017-06-06
Angular依赖注入系统里Injection token PLATFORM_ID使用场景详解
这篇文章主要为大家介绍了Angular依赖注入系统里Injection token PLATFORM_ID使用场景详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-11-11
Angularjs中$http以post请求通过消息体传递参数的实现方法
这篇文章主要介绍了Angularjs中$http以post请求通过消息体传递参数的方法,结合实例形式分析了$http使用post请求传递参数的相关设置与使用技巧,需要的朋友可以参考下2016-08-08
Angular4学习之Angular CLI的安装与使用教程
网上很多教程过时,命令在angular4中不适用等等,所以下面这篇文章主要给大家介绍了关于Angular4学习之Angular CLI的安装与使用教程的相关资料,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。2018-01-01


最新评论