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没提供的需求的时候,就可以自定义个指令.指令的好处显而易 ...
随机推荐
- Scala基础语言api入门学习
Scala的变量定义 变量定义 声明一个val变量类似与java的 public static final String 一致,一旦初始化不能改变,和java的泛型类似,Scala会帮我们进行类型推断 ...
- LaTeX宏包TikZ绘图示例——Go语言起源图
本例所绘图形选自<Go语言程序设计>(作者:Alan A. A. Donovan与Brian W. Kernighan)一书的前言部分. 完整代码 \documentclass{art ...
- Codeforces VK Cup 2018 Div.2
总题面传送门 这次考试只过了3题,前三题题目难度并不大,但是第三题的代码细节卡了我两个半小时(基本上整场考试),所以以后要合理把握时间,注意把握代码细节,并更加完善我的代码风格,使其更加简练.(赛外话 ...
- CUDA基础介绍
一.GPU简介 1985年8月20日ATi公司成立,同年10月ATi使用ASIC技术开发出了第一款图形芯片和图形卡,1992年4月ATi发布了Mach32图形卡集成了图形加速功能,1998年4月ATi ...
- burp抓取手机包
burp监听ip和端口要填对应的使用的ip,比如建立了一个网卡wifi,那就填那个ip 手机代理连接wifi的话,就直接先看能不能访问burp监听的端口,然后填上相同代理即可.
- lua编程之协程介绍
一,lua协程简介 协程(coroutine),意思就是协作的例程,最早由Melvin Conway在1963年提出并实现.跟主流程序语言中的线程不一样,线程属于侵入式组件,线程实现的系统称之为抢占式 ...
- python简单计时器实现
实现程序运行时间的显示与相互之间的计算: 实现代码: import time as t class Mytimer(): def __init__(self): self.unit=["年& ...
- 执行sh脚本报“/usr/bin/env: "sh\r": 没有那个文件或目录”错误
出现这个错误的原因是出错的语句后面多了“\r”这个字符,换言之,脚本文件格式的问题,我们只需要把格式改成unix即可: vi xx.sh :set ff :set ff=unix :wq!
- Python读取文件编码解码问题
用chardet检测编码 import chardet raw = open("model.json", 'rb').read() result = chardet.detect( ...
- Unity面试技巧之C#基础
1. 定义常量最好使用运行是常量就是readonly 编译常量就是 const public static readonly MyClass myClass = new MyClass(); publ ...