在項目開發中咱們常常會遇到圖片輪播的功能點:javascript
若是咱們開發人員本身原生手寫,將會花費不少的時間,最終得不償失。html
接下來就詳細說說如何使用angular-ui發熱圖片輪播模塊,而且將它寫成一個指令(便於複用)java
一如既往的咱們項目中使用時requireJS進行js代碼的編譯git
準備工做:github
1):引入angularJS , ui-bootstrap-tpls-1.3.2(我使用的是1.3.2版本的)bootstrap
第一步:本身寫一個指令(命名爲picchange)app
說明:指令控制器中的代碼都是angualr-ui官網上拷貝的(由於此文章的重點是如何將其封裝成指令,其餘的不作重點)dom
指令的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(這個沒啥理解的)curl
指令的html
<div> <div style="height: 305px"> <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}}" style="margin:auto;"> <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>
到此爲止關於指令的封裝已經完成,接下來是如何使用的問題:ide
(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/