用AngularJS的指令實(shí)現(xiàn)tabs切換效果
先來看看效果圖
首先先來說一下指令嵌套,指令嵌套顧名思義就是兩個(gè)以上的指令嵌套在一起,比如下面這樣:
<A-deirective> <B-directive></B-directive> <C-directive></C-directive> </A-directive>
下面這個(gè)tabs功能的指令,剛好用到了這個(gè),可以讓代碼更加簡(jiǎn)潔。
<!DOCTYPE html> <html lang="en" ng-app="docsTabsExample"> <head> <meta charset="UTF-8"> <title></title> <script></script> <script src="lib/angular.min.js" type="text/javascript"></script> <script src="lib/angular-route.js" type="text/javascript"></script> <script src="lib/jquery-2.1.4.min.js"></script> <script src="lib/bootstrap.js" type="text/javascript"></script> <link rel="stylesheet" href="css/bootstrap.css" type="text/css"/> <style> .active{ background: red; } </style> </head> <body ng-controller="appCon"> <my-tabs><!--最外層指令--> <my-pane tittle="ONE"><!--內(nèi)層指令--> <h4>One</h4> <p>angularangularangularangularangularangularangular</p> </my-pane> <my-pane tittle="TWO"><!--內(nèi)層指令--> <h4>Two</h4> <p>angularangularangularangularangularangularangular</p> </my-pane> <my-pane tittle="THERE"><!--內(nèi)層指令--> <h4>There</h4> <p>bootstrapbootstrapbootstrapbootstrapbootstrapbootstrap</p> </my-pane> <my-pane tittle="FIVE"><!--內(nèi)層指令--> <h4>five</h4> <p>jqueryjqueryjqueryjqueryjqueryjqueryjquery</p> </my-pane> </my-tabs> </body> <script> var app=angular.module("docsTabsExample",['template']) .controller("appCon",["$scope",function($scope){ }]) .directive("myTabs", function () { return{ restrict:"EA", transclude: true, scope:{}, templateUrl:"myTabs.html", controller:["$scope", function ($scope) {//使用controller讓最內(nèi)層指令來繼承外層指令,這樣內(nèi)層就可以通過scope的傳導(dǎo),來與外層指令進(jìn)行數(shù)據(jù)之間的傳遞 var panes = $scope.scopes = [];// $scope.select= function (pane) {//實(shí)現(xiàn)tabs功能 angular.forEach(panes, function (scope) { //遍歷所有內(nèi)存指令scope,統(tǒng)一隱藏內(nèi)容。 scope.selected=false; }); pane.selected=true;//通過ng-repeat只 }; this.addScope= function (scope) {//由內(nèi)層指令來繼承,把內(nèi)層指令的scope,傳到進(jìn)外層指令進(jìn)行控制 if(panes.length===0){ $scope.select(scope); } panes.push(scope);//把內(nèi)層指令數(shù)組,傳入外層指令scope數(shù)組。 } }] } }) .directive("myPane", function () { return{ restrict:'EA', scope:{ tittle:'@' }, transclude: true, require:'^myTabs',//繼承外層指令 templateUrl:"myPane.html", link: function (scope, elemenet,attrs,myTabsController) { myTabsController.addScope(scope);//把內(nèi)層指令的scope存入到外層指令中,讓外層遍歷。 } } }); angular.module("template",[]) .run(["$templateCache", function ($templateCache) { $templateCache.put("myTabs.html","<div class='table'>" + "<ul class='nav nav-tabs'>" + "<li ng-repeat='pane in scopes' ng-class='{active:pane.selected}'>" + "<a href='#' ng-click='select(pane)'>{{pane.tittle}}<a/>" + "</li>" + "</ul>" + "<div ng-transclude class='tab-content'></div>" + "</div>") }]) .run(["$templateCache", function ($templateCache) { $templateCache.put("myPane.html","<div class='table-pane' ng-show='selected' ng-transclude>" + "</div>") }]) </script> </html>
整個(gè)指令的的實(shí)現(xiàn)原理就是通過指令的繼承,把內(nèi)層指令的scope
暴露到外層指令中,這樣就可以在外層指令控制內(nèi)層指令的顯示。這里還有另外一個(gè)要說明的,為了讓指令更加有層次,更加有邏輯性,使用了ng-transclude
,讓指令的template
內(nèi)容,反向插入到標(biāo)有ng-transclude
的內(nèi)容中。
結(jié)束語(yǔ)
好了,以上就是AngularJS通過指令實(shí)現(xiàn)tabs切換功能的全部?jī)?nèi)容,大家都學(xué)會(huì)了嗎?希望對(duì)大家學(xué)習(xí)AngularJS能有所幫助,如果有疑問可以留言交流。謝謝大家對(duì)腳本之家的支持。
- AngularJS實(shí)現(xiàn)動(dòng)態(tài)切換樣式的方法分析
- AngularJS標(biāo)簽頁(yè)tab選項(xiàng)卡切換功能經(jīng)典實(shí)例詳解
- 詳解AngularJS ng-class樣式切換
- angularJs的ng-class切換class
- 使用AngularJS2中的指令實(shí)現(xiàn)按鈕的切換效果
- AngularJS路由切換實(shí)現(xiàn)方法分析
- AngularJS實(shí)現(xiàn)使用路由切換視圖的方法
- AngularJS入門教程之多視圖切換用法示例
- 使用AngularJS實(shí)現(xiàn)可伸縮的頁(yè)面切換的方法
- angularjs實(shí)現(xiàn)Tab欄切換效果
相關(guān)文章
AngularJS實(shí)現(xiàn)的獲取焦點(diǎn)及失去焦點(diǎn)時(shí)的表單驗(yàn)證功能示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)的獲取焦點(diǎn)及失去焦點(diǎn)時(shí)的表單驗(yàn)證功能,涉及AngularJS使用ng-blur、ng-focus針對(duì)表單事件監(jiān)聽相關(guān)操作技巧,需要的朋友可以參考下2017-10-10angular 數(shù)據(jù)綁定之[]和{{}}的區(qū)別
這篇文章主要介紹了angular 數(shù)據(jù)綁定之[]和{{}}的區(qū)別,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09在 Angular-cli 中使用 simple-mock 實(shí)現(xiàn)前端開發(fā) API Mock 接口數(shù)據(jù)模擬功能的方法
這篇文章主要介紹了在 Angular-cli 中使用 simple-mock 實(shí)現(xiàn)前端開發(fā) API Mock 接口數(shù)據(jù)模擬功能的方法,需要的朋友可以參考下2018-11-11AngularJS基礎(chǔ) ng-non-bindable 指令詳細(xì)介紹
本文主要講解AngularJS ng-non-bindable 指令,這里幫大家整理了ng-non-bindable指令的基本知識(shí)資料,有需要的小伙伴可以參考下2016-08-08