如何将angular-ui-bootstrap的图片轮播组件封装成一个指令?

在项目开发中我们经常会遇到图片轮播的功能点:

如果我们开发人员自己原生手写,将会花费很多的时间,最终得不偿失。

接下来就详细说说如何使用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 > <uib-carousel active="active" interval="myInterval" no-wrap="noWrapSlides"> <uib-slide ng-repeat="slide in slides track by slide.id" index="slide.id"> <img 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 type="button" class="btn btn-info" ng-click="addSlide()">Add Slide</button> <button type="button" class="btn btn-info" 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 type="number" class="form-control" ng-model="myInterval"> <br />Enter a negative number or 0 to stop the interval. </div> </div> </div> 

到此为止关于指令的封装已经完成,接下来是如何使用的问题:

(1)有一个页面要用到此指令:(命名为test.html)

<p>图片的轮播</p>
<div picchange picurl='img'></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/