angularJS1笔记-(15)-自定义指令(accordion伸缩菜单原始实现)
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="../../vendor/bootstrap3/css/bootstrap.min.css"/>
</head>
<body>
<div ng-app="myApp">
<!--container为居中的div-->
<div class="container">
<div ng-controller="firstController">
<kittencup-group>
<kittencup-collapse ng-repeat="collapse in data" heading="{{collapse.title}}">
{{collapse.content}}
</kittencup-collapse>
</kittencup-group>
</div>
</div>
</div> <script type="text/javascript" src="../../vendor/angular/angularJs.js"></script>
<script type="text/javascript" src="app/index.js"></script> <script>
</script> </body>
</html>
kittencupCollapse.html
<div class="panel panel-default">
<div class="panel-heading" ng-click="changeStatus()">
<h4 class="panel-title">
<a href="#">
{{heading}}
</a>
</h4>
</div>
<!--collapse为true 才会隐藏子内容-->
<div class="panel-collapse" ng-class="{collapse:!isOpen}">
<div class="panel-body" ng-transclude>
</div>
</div>
</div>
index.js:
var myApp = angular.module('myApp', [])
//数据
.factory('myData', function () {
return [
{title: "no1", content: "no1-content1"},
{title: "no2", content: "no2-content2"},
{title: "no3", content: "no3-content3"}
];
})
//控制器
.controller('firstController', ['$scope', 'myData', function ($scope, myData) {//把myData注入进来
$scope.data = myData;
}])
.directive('kittencupGroup', function () {
return {
restrict: 'E',
replace: true,
template: '<div class="panel-group" ng-transclude></div>',//此处为kittencup-group标签里面的内容占位成一个panel-group
transclude: true,
controllerAs: 'kittencuupGroupController',
controller: function () {
this.groups = [];
//关闭其他的
this.closeOtehrCollapse = function (nowScope) {
angular.forEach(this.groups, function (scope) {
if (scope != nowScope) {
scope.isOpen = false;
}
})
}
}
}
})
.directive('kittencupCollapse', function () {
return {
restrict: 'E',
replace: true,
require: '^kittencupGroup',
templateUrl: 'temp/kittencupCollapse.html',
scope: {
heading: "@"
},
//link可以把其他的controller依赖注入进来
link: function (scope, element, attrs, kittencuupGroupController) {
scope.isOpen = false;
scope.changeStatus = function () {
scope.isOpen = !scope.isOpen;
kittencuupGroupController.closeOtehrCollapse(scope);
}
kittencuupGroupController.groups.push(scope);
},
transclude: true //将模板的内容放在指定的ng-transclude属性的标签里面去
}
})
运行结果:

angularJS1笔记-(15)-自定义指令(accordion伸缩菜单原始实现)的更多相关文章
- angularJS1笔记-(11)-自定义指令(transclude/priority/terminal)
自定义指令的属性 transclude:为true时,允许把html中新定义的指令中原来的dom运用到该指令的template中. 属性priority,设置该指令的优先级,优先级大的先执行,默认指令 ...
- angularJS1笔记-(10)-自定义指令(templateUrl属性)
index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- angularJS1笔记-(9)-自定义指令(restrict/template/replace)
index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- angularJS1笔记-(14)-自定义指令(scope)
index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- angularJS1笔记-(13)-自定义指令(controller和controllerAs实现通信)
index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- angularJS1笔记-(12)-自定义指令(compile/link)
index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- Vue笔记--通过自定义指令实现按钮操作权限
经常做中后台系统,此类系统的权限是比较重要,拿自己做过的一些项目做个笔记. Vue实现的中后台管理系统.按钮操作权限的空置一般都是通过自定义指令Vue.directive. <el-button ...
- AngularJs学习笔记3——自定义指令
指令 概述: 前面也说过一些常用指令,用于快速入门.现在详细总结一下:指令用于实现各种页面的操作,是对于底层DOM操作的封装,扩展了HTML的行为,实现页面交互以及数据绑定. 指令是一种执行的信号,一 ...
- AngularJS学习笔记(四) 自定义指令
指令(directive)是啥?简单来说就是实现一定功能的XXX...之前一直用的ng-model,ng-click等等都是指令.当我有一个ng没提供的需求的时候,就可以自定义个指令.指令的好处显而易 ...
随机推荐
- Openssl自建CA
查看证书相关指令 # 查看公钥数字证书 openssl x509 -in cacert.pem -noout -text # 查看私钥数字证书 openssl pkcs12 -in client-ce ...
- 十张图了解Docker【转】
这篇文章希望能够帮助读者深入理解Docker的命令,还有容器(container)和镜像(image)之间的区别,并深入探讨容器和运行中的容器之间的区别. 当我对Docker技术还是一知半解的时候,我 ...
- d3 js emberjs handlerbarjs
http://handlebarsjs.com/ http://emberjs.com/ http://jsbin.com/d3ember-barchart/13/edit?html,output
- jQuery学习-访问设置元素内容
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【HEOI2016】序列
题面 题解 很像最长不下降子序列对吧(废话) 设$up[i]$和$down[i]$分别表示$i$最大最小能取多少 注意到: $$ f[i] = max_j\left\{f[j]\right\} + 1 ...
- Gitlab+Jenkins学习之路(三)之gitlab权限管理--issue管理
1.创建Group,User,Project 创建一个组,组名为java Group path http://192.168.56.11/java Visibility Level: #为权限级别,一 ...
- python基础—字典
阅读文本需要3分钟,不建议跳读 节目清单 字典是python中最重要的数据类型,字典由“键-值”对组成的集合,字典中的“值”通过“键”来引用.这里将介绍字典的定义.访问.排序等功能. 字典的创建 字典 ...
- [C++]linux下实现ls()函数遍历目录
转载请注明原创:http://www.cnblogs.com/StartoverX/p/4600794.html 需求:在linux下遍历目录,输出目录中各文件名. 在linux下遍历目录的相关函数有 ...
- 人工智能AI芯片与Maker创意接轨(下)
继「人工智能AI芯片与Maker创意接轨」的(上)篇中,认识了人工智能.深度学习,以及深度学习技术的应用,以及(中)篇对市面上AI芯片的类型及解决方案现况做了完整剖析后,系列文到了最后一篇,将带领各位 ...
- Unity摄像机围绕物体旋转两种实现方式
第一种,使用Transform 函数 RotateAround. 代码如下: public Transform target;//获取旋转目标 private void camerarotate() ...