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没提供的需求的时候,就可以自定义个指令.指令的好处显而易 ...
随机推荐
- 北京Uber优步司机奖励政策(4月25日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Ceph学习之路(三)Ceph luminous版本部署
1.配置ceph.repo并安装批量管理工具ceph-deploy [root@ceph-node1 ~]# vim /etc/yum.repos.d/ceph.repo [ceph] name=Ce ...
- imu标定 imu_tk
1. 首先标定加速度计,这是imu加速度计xyz三个轴在标定过程中的读数: 标定结果: 2. 利用加速度计的标定结果,标定陀螺仪,结果: 也可以使用港科大开源的一个工具: https://github ...
- 实验二:ICMP重定向攻击
-:实验原理 ICMP重定向信息是路由器向主机提供实时的路由信息,当一个主机收到ICMP重定向信息时,它就会根据这个信息来更新自己的路由表.由于缺乏必要的合法性检查,如果一个黑客想要被攻击的主机修改它 ...
- Python 学习 第五篇:语句和语法
Python程序是语句构成的,语句包含表达式,表达式嵌套在语句中,包含变量和常量,用于处理对象.Python的语法实质上是由表达式.语句和代码块构成的.语句是由表达式构成的,代码块是由多个语句构成的复 ...
- 手把手教你测微信小程序
WeTest 导读 在小程序持续大量爆发的形势下,现在已经成为了各平台竞争的战略布局重点.至今年2月,月活超500万的微信小程序已经达到237个,其中个人开发占比高达2成.因小程序的开发门槛低.传播快 ...
- stringObject.substring(start,stop)
用于提取字符串中 介于两个指定下标之间的字符. start 必需.一个非负的整数 stop 可选.一个非负的整数
- ASP.NET MVC - PageData的应用
一.要实现一个功能,在不同的页面放置一段如下的内容,用于采集用户行为信息: <input type='hidden' id='page_id' value='xxxx' /> <sc ...
- 禁用AxWebBrowser右键菜单
出处:http://stackoverflow.com/questions/41781647/disabling-the-axwebbrowser-context-menu-vb-net 通过底层消息 ...
- Mysql试题集锦
1.一张表,里面有 ID 自增主键,当 insert 了 17 条记录之后,删除了第 15,16,17 条记录,再把 Mysql 重启,再 insert 一条记录,这条记录的 ID 是 18 还是 1 ...