AngularJS實現標籤頁的兩種方式_AngularJS

來源:互聯網
上載者:User

一、通過普通指令實現標籤頁

<link rel="stylesheet" href="views/show/tab.css"/><div> <ul class="nav nav-tabs" ng-init="vm.activeTab=1"> <li ng-class="{active: vm.activeTab == 1}"><a href="javascript:;" ng-click="vm.activeTab = 1">標籤1</a></li> <li ng-class="{active: vm.activeTab == 2}"><a href="javascript:;" ng-click="vm.activeTab = 2">標籤2</a></li> </ul> <div class="tab-content tab-bordered"> <div class="tab-panel" ng-show="vm.activeTab == 1"> 標籤1的內容 </div> <div class="tab-panel" ng-show="vm.activeTab == 2"> 標籤2的內容 </div> </div></div><h3>說明</h3>這裡示範的是直接通過bootstrap實現的方法。<hr/>還可以通過angular-bootstrap的tabset指令實現,參見 <a href="http://angular-ui.github.io/bootstrap/#/tabs" target="_blank">官方Demo</a>'use strict';angular.module('ngShowcaseApp').controller('ctrl.show.tab', function ($scope) { var vm = $scope.vm = {};});.tab-content.tab-bordered { border: 1px solid lightgray; border-top: none; padding: 15px; border-radius: 0 0 4px 4px;}

二、自訂指令實現的標籤頁

<!DOCTYPE html><html lang="en" ng-app="demo"><head> <meta charset="UTF-8"> <title></title> <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> .btn-group{  position: relative;  left: 40px; } .list-group{  position: relative;  left: 0; } .list-group-item{ } #list3{  width: 200px; } </style></head><body > <div ng-controller="directiveControl"> <div style="width: 100px;height: 100px;border: 1px solid blue" ng-class="{'hidden':value}" ></div> <div>  <list ng-model="value" ></list> </div> <script type="text/ng-template" id="list.html">  <div >  <div class="btn-group">   <ul class="nav nav-tabs">   <li role="presentation" ng-mouseover="flag=3" ng-mouseleave="flag=4"><a href="#" >{{name}}</a></li>   </ul>  </div>  <div class="list-group" id="list3" ng-show="flag==3" ng-mouseover="flag=3" ng-mouseleave="flag=4">   <ul >   <li class="list-group-item " ng-click="fun1()"><a href="#">Action</a></li>   <li class="list-group-item "><a href="#">Another action</a></li>   <li class="list-group-item "><a href="#">Something else here</a></li>   <li class="list-group-item "><a href="#">Separated link</a></li>   </ul>  </div>  </div> </script> </div></body><script> var demo=angular.module("demo",[]); demo.controller("directiveControl", function ($scope) { }); demo.directive("list",[function () { return {  restrict:'EA',  templateUrl:'list.html',  scope:{  value:'=ngModel'  },  link: function (scope,element,attr) {  scope.name="home";  scope.lists=[{name:'home'},{name:'family '}];  scope.fun1= function () {   scope.value=true;   console.log("a")  }  } } }])</script></html>

(1)首先要解決指令必須寫在一個根標籤中,一定要用div包裹
(2)指令外部傳遞參數要使用ng-model,來聲明變數,
         在指令中用scope:{
         value:'ngModel'
         }來賦值

總結

以上就是關於AngularJS實現標籤頁的全部內容,希望這篇文章對大家學習或使用AngularJs能有所協助,如果有疑問大家可以留言交流。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.