AngularJS实现标签页的两种方式
一、通过普通指令实现标签页
<linkrel="stylesheet"href="views/show/tab.css"/>
<div>
<ulclass="navnav-tabs"ng-init="vm.activeTab=1">
<ling-class="{active:vm.activeTab==1}"><ahref="javascript:;"ng-click="vm.activeTab=1">标签1</a></li>
<ling-class="{active:vm.activeTab==2}"><ahref="javascript:;"ng-click="vm.activeTab=2">标签2</a></li>
</ul>
<divclass="tab-contenttab-bordered">
<divclass="tab-panel"ng-show="vm.activeTab==1">
标签1的内容
</div>
<divclass="tab-panel"ng-show="vm.activeTab==2">
标签2的内容
</div>
</div>
</div>
<h3>说明</h3>
这里演示的是直接通过bootstrap实现的方法。
<hr/>还可以通过angular-bootstrap的tabset指令实现,参见<ahref="http://angular-ui.github.io/bootstrap/#/tabs"target="_blank">官方Demo</a>
'usestrict';
angular.module('ngShowcaseApp').controller('ctrl.show.tab',function($scope){
varvm=$scope.vm={};
});
.tab-content.tab-bordered{
border:1pxsolidlightgray;
border-top:none;
padding:15px;
border-radius:004px4px;
}
二、自定义指令实现的标签页
<!DOCTYPEhtml>
<htmllang="en"ng-app="demo">
<head>
<metacharset="UTF-8">
<title></title>
<scriptsrc="lib/angular.min.js"type="text/javascript"></script>
<scriptsrc="lib/angular-route.js"type="text/javascript"></script>
<scriptsrc="lib/jquery-2.1.4.min.js"></script>
<scriptsrc="lib/bootstrap.js"type="text/javascript"></script>
<linkrel="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>
<divng-controller="directiveControl">
<divstyle="width:100px;height:100px;border:1pxsolidblue"ng-class="{'hidden':value}"></div>
<div>
<listng-model="value"></list>
</div>
<scripttype="text/ng-template"id="list.html">
<div>
<divclass="btn-group">
<ulclass="navnav-tabs">
<lirole="presentation"ng-mouseover="flag=3"ng-mouseleave="flag=4"><ahref="#">{{name}}</a></li>
</ul>
</div>
<divclass="list-group"id="list3"ng-show="flag==3"ng-mouseover="flag=3"ng-mouseleave="flag=4">
<ul>
<liclass="list-group-item"ng-click="fun1()"><ahref="#">Action</a></li>
<liclass="list-group-item"><ahref="#">Anotheraction</a></li>
<liclass="list-group-item"><ahref="#">Somethingelsehere</a></li>
<liclass="list-group-item"><ahref="#">Separatedlink</a></li>
</ul>
</div>
</div>
</script>
</div>
</body>
<script>
vardemo=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能有所帮助,如果有疑问大家可以留言交流。