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伸缩菜单原始实现)的更多相关文章

  1. angularJS1笔记-(11)-自定义指令(transclude/priority/terminal)

    自定义指令的属性 transclude:为true时,允许把html中新定义的指令中原来的dom运用到该指令的template中. 属性priority,设置该指令的优先级,优先级大的先执行,默认指令 ...

  2. angularJS1笔记-(10)-自定义指令(templateUrl属性)

    index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  3. angularJS1笔记-(9)-自定义指令(restrict/template/replace)

    index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  4. angularJS1笔记-(14)-自定义指令(scope)

    index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  5. angularJS1笔记-(13)-自定义指令(controller和controllerAs实现通信)

    index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  6. angularJS1笔记-(12)-自定义指令(compile/link)

    index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  7. Vue笔记--通过自定义指令实现按钮操作权限

    经常做中后台系统,此类系统的权限是比较重要,拿自己做过的一些项目做个笔记. Vue实现的中后台管理系统.按钮操作权限的空置一般都是通过自定义指令Vue.directive. <el-button ...

  8. AngularJs学习笔记3——自定义指令

    指令 概述: 前面也说过一些常用指令,用于快速入门.现在详细总结一下:指令用于实现各种页面的操作,是对于底层DOM操作的封装,扩展了HTML的行为,实现页面交互以及数据绑定. 指令是一种执行的信号,一 ...

  9. AngularJS学习笔记(四) 自定义指令

    指令(directive)是啥?简单来说就是实现一定功能的XXX...之前一直用的ng-model,ng-click等等都是指令.当我有一个ng没提供的需求的时候,就可以自定义个指令.指令的好处显而易 ...

随机推荐

  1. MIPS架构——汇编代码转机器代码编译器 Matlab GUI

    MIPS架构下的MCU,指令集包含R-Type.I-Type.J-Type三种,在数电课程设计时为了给MCU编写指令集,需要将汇编语言转化成机器代码,这里分享一下自己写的Matlab 的 GUI. 主 ...

  2. AGC 005 D - ~K Perm Counting

    D - ~K Perm Counting 链接 题意: 求有多少排列对于每个位置i都满足$|ai−i|!=k$.n<=2000 分析: 容斥+dp. $answer = \sum\limits_ ...

  3. opencv-Getting Started with Images

    1.opencv库简单的操作图片 # coding = utf-8 # 书籍:<<学习opencv>> import cv2 from matplotlib import py ...

  4. cogs1799 [国家集训队2012]tree(伍一鸣)

    LCT裸题 注意打标记之间的影响就是了 这个膜数不会爆unsigned int #include<cstdio> #include<cstdlib> #include<a ...

  5. cogs 1330 [HNOI2008]玩具装箱toy

    cogs 1330 [HNOI2008]玩具装箱toy 瞎扯,急忙AC的请跳过 感觉数据结构写的太多了有点晕=+ 发现还没学斜率优化+- 于是来学一学QwQ 上次这题打了个决策优化直接水过了..理论O ...

  6. Flutter - 弹出底部菜单Show Modal Bottom Sheet

    在很多安卓App上,有很多底部弹出的菜单,这个在Flutter上同样可以实现. 先看一下效果 嗯,就是这样子的,当用户点击菜单区域以外的时候,菜单会自动关闭. 下面就看一下Dart语言实现 float ...

  7. 定时任务的使用-crond

    第1章 练习题 1.1 每天晚上 12 点,打包站点目录/var/www/html 备份到/data 目录下(最好每次备份按时间生成不同的备份包) 1.1.1 测试命令 [root@znix ~]# ...

  8. 用xpo实现dc技术的关键点-XPO是如何处理接口类型与真实类型的对应关系的

    https://www.devexpress.com/Support/Center/Question/Details/Q487000/xpodatamodel-and-model-interfaces ...

  9. requestAnimationFrame优势何在?

    大概半年前,无意中在网上看到一个新的js函数requestAnimationFrame,据说,此函数可以优化传统的js动画效果,似乎是未来js动画的新方向. 当时我所在的项目正好用到了和js动画有关的 ...

  10. [翻译] Python 3.5中async/await的工作机制

    Python 3.5中async/await的工作机制 多处翻译出于自己理解,如有疑惑请参考原文 原文链接 身为Python核心开发组的成员,我对于这门语言的各种细节充满好奇.尽管我很清楚自己不可能对 ...