詳解如何將angular-ui的圖片輪播組件封裝成一個指令
在項目開發(fā)中我們經常會遇到圖片輪播的功能點:
如果我們開發(fā)人員自己原生手寫,將會花費很多的時間,最終得不償失。
接下來就詳細說說如何使用angular-ui發(fā)熱圖片輪播模塊,并且將它寫成一個指令(便于復用)
一如既往的我們項目中使用時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;//是否循環(huán)輪播
$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是用來傳遞參數(shù)的-->
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.js實現(xiàn)動態(tài)加載組件詳解
這篇文章主要給大家介紹了關于Angular.js實現(xiàn)動態(tài)加載組件的相關資料,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-05-05
angular2實現(xiàn)統(tǒng)一的http請求頭方法
今天小編就為大家分享一篇angular2實現(xiàn)統(tǒng)一的http請求頭方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
AngularJs Understanding Angular Templates
本文主要介紹AngularJs Understanding Angular Templates的資料,這里整理了詳細的資料及簡單示例代碼,有興趣的小伙伴的參考下2016-09-09

