第216天:Angular---自定义指令(二)
自定义指令
1、第一个参数是指令的名字,第二个参数任然应该使用一个数组,数组的最后一个元素是一个函数。定义指令的名字,应该使用驼峰命名法
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">
</head> <body ng-app="demoApp">
<!-- <newsButton></newsButton> -->
<!-- <news-button></news-button> -->
<!-- <div newsButton></div> -->
<btn-primary></btn-primary>
<btn-danger></btn-danger>
<script src="bower_components/angular/angular.js"></script>
<script>
var demoApp = angular.module('demoApp', []); // 第一个参数是指令的名字,第二个参数任然应该使用一个数组,数组的最后一个元素是一个函数
// 定义指令的名字,应该使用驼峰命名法
demoApp.directive('newsButton', [function() {
// 该函数应该返回一个指令对象
return {
template:'<input type="button" value="news" class="btn btn-lg btn-primary btn-block" />'
};
}]); // demoApp.directive('btnPrimary', [function() {
// return {
// template:'<input type="button" value="news" class="btn btn-primary" />'
// };
// }]); // demoApp.directive('btnDanger', [function() {
// return {
// template:'<input type="button" value="news" class="btn btn-danger" />'
// };
// }]); // demoApp.directive('btnSuccess', [function() {
// return {
// template:'<input type="button" value="news" class="btn btn-success" />'
// };
// }]); demoApp.controller('DemoController', ['$scope', function($scope) {
// $scope.xxxx=xxx;
// $scope.do=function() { // };
// $scope.$watch('',function(now,old) { // });
}]);
</script>
</body> </html>
2、自定义一个面包屑导航
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">
</head> <body ng-app="demoApp">
<!-- <btn>itcast</btn> -->
<div breadcrumb></div>
<breadcrumb data=""></breadcrumb>
<script src="bower_components/angular/angular.js"></script>
<script>
var demoApp = angular.module('demoApp', []); demoApp.directive('breadcrumb', [function() {
// Runs during compile
return {
// 指定当前指令的类型什么样的
// restrict: 'EA',
// // E = Element, A = Attribute, C = Class, M = Comment
// template: '', // 模版字符串
templateUrl: 'tmpls/breadcrumb.html',
replace: true,
// transclude: true,
};
}]); // demoApp.directive('btn', [function() {
// return{
// scope:{
// primary:'@',
// lg:'@',
// block:'@',
// },
// template:'<button class="btn {{primary==\'true\'?\'btn-primary\':\'\'}}">button</button>'
// }
// }]); // demoApp.directive('btn', [function() {
// return {
// // 指令对象的transclude必须设置为true才可以在模版中使用ng-transclude指令
// transclude: true,
// replace: true, // 替换指令在HTML中绑定的元素
// template: '<button class="btn btn-primary btn-lg" ng-transclude></button>'
// };
// }]);
</script>
</body> </html>
3、面包屑导航
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>封装一个面包屑导航</title>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">
</head> <body ng-app="myApp" ng-controller="DemoController">
<breadcrumb data="{{pathData1}}"></breadcrumb>
<breadcrumb data="{{pathData2}}"></breadcrumb>
<script src="bower_components/angular/angular.js"></script>
<script>
var myApp = angular.module('myApp', []); myApp.controller('DemoController', ['$scope', function($scope) {
$scope.pathData1 = {
home: '#',
news: '#',
itheima: '#',
bbs: '#'
};
$scope.pathData2 = {
home: '#',
library: '#',
data: '#'
};
}]); // 定义一个面包屑导航指令
myApp.directive('breadcrumb', [function() {
// 返回指令对象
return {
scope: {},
templateUrl: 'tmpls/breadcrumb.html',
replace: true,
link: function(scope, element, attributes) {
scope.data = JSON.parse(attributes.data);
// console.log(scope.data);
}
};
}]);
</script>
</body> </html>
第216天:Angular---自定义指令(二)的更多相关文章
- Angular自定义指令(directive)
angular自定义指令,意我们可以通过angula自己定义指令,来实现我们的特殊要求,为所欲为,一支穿云箭,千军万马来相见 多少年的老规矩了,先看代码: <!DOCTYPE html> ...
- angular 自定义指令详解 Directive
在angular中,Directive,自定义指令的学习,可以更好的理解angular指令的原理,当angular的指令不能满足你的需求的时候,嘿嘿,你就可以来看看这篇文章,自定义自己的指令,可以满足 ...
- Angular自定义指令directive:scope属性
在AngularJS中,除了内置指令如ng-click等,我们还可以自定义指令.自定义指令,是为了扩展DOM元素的功能.代码中,通过指定directive中的restrict属性,来决定这个指令是作为 ...
- angular 自定义指令 directive transclude 理解
项目中断断续续的用了下angular,也没狠下心 认真的学习.angular 特别是自定义指令这块 空白. transclude 定义是否将当前元素的内容转移到模板中.看解释有点抽象. 看解释有点抽象 ...
- Angular17 Angular自定义指令
1 什么是HTML HTML文档就是一个纯文本文件,该文件包含了HTML元素.CSS样式以及JavaScript代码:HTML元素是由标签呈现,浏览器会为每个标签创建带有属性的DOM对象,浏览器通过渲 ...
- angular自定义指令
1.在directive文件下创建指令的js文件 通常自定义指令需要声明模块(注意定义指令时, js内部指令名称需采用 aaAaBb驼峰的命名方式 html中使用的是aa-aa-bb) e.g (f ...
- angular -- 自定义指令和模板
angular 可以自定义一些指令,来简化我们前端的工作量. 第一种:简单指令示例: <h3>自定义指令</h3> <sheng></sheng> &l ...
- angular自定义指令命名的那个坑
Directive 先从定义一个简单的指令开始. 定义一个指令本质上是在HTML中通过元素.属性.类或注释来添加功能.AngularJS的内置指令都是以ng开头,如果想自定义指令,建议自定义一个前缀代 ...
- angular自定义指令相关知识及代码
原文地址 https://www.jianshu.com/p/0c015862156d 大纲 1.自定义指令之——属性指令 2.自定义属性指令的运行原理 3.自定义属性指令代码实践 4.自定义结构指令 ...
- angular自定义指令 repeat 循环结束事件;limitTo限制循环长度、限定开始位置
1.获取repeat循环结束: 自定义指令: .directive('repeatFinish', function () { return { link: function (scope, elem ...
随机推荐
- Apache Flink学习笔记
Apache Flink学习笔记 简介 大数据的计算引擎分为4代 第一代:Hadoop承载的MapReduce.它将计算分为两个阶段,分别为Map和Reduce.对于上层应用来说,就要想办法去拆分算法 ...
- 【10.13】Bug Bounty Write-up 总结
今天惯例邮箱收到了Twitter的邮件提醒有新的post,这种邮件每天都能收到几封,正好看到一个Bug Bounty的write up,比较感兴趣,看起来也在我的理解范围之内,这里对这篇write u ...
- 查找linux镜像源中的软件版本并进行安装
输入以下代码进行软件查找 sudo apt-cache search YourSoftwareName 根据所得到的结果进行安装 sudo apt-get install YourSoftwareNa ...
- 阿里路由框架ARouter的使用步骤
ARouter的使用步骤(以宿主APP modulebase和moduleuser 三大模块组成的工程为例) 第一步 因为路由跳转是子模块都需要用到的,所以我们在module_base模块中引入 co ...
- .Net MVC缓存
https://www.cnblogs.com/JoeSnail/p/7993903.html
- MySQL两种引擎的比较
MyISAM,InnoDB主要区别: 1.MyISAM是非事物安全的,InnoDB是事物安全的. 事物安全的特点为更安全,遇到问题会自动恢复或从备份加事物日志回复,如果更新失败,你的所有改变都变回原来 ...
- vscode调试js,安装了nodejs之后还出现无法在Path上找到运行时的node
vscode 调试js,安装了nodejs之后还出现无法在Path上找到运行时的node. 重启vscode解决
- Netty源码分析第8章(高性能工具类FastThreadLocal和Recycler)---->第4节: recycler中获取对象
Netty源码分析第八章: 高性能工具类FastThreadLocal和Recycler 第四节: recycler中获取对象 这一小节剖析如何从对象回收站中获取对象: 我们回顾上一小节demo的ma ...
- DockerCon2017前瞻 - Docker企业版体验
DockerCon 2017将于四月17号在美国Austin召开.在去年DockerCon上,Docker公司一系列的发布吹响了进军企业市场的号角.今天,容器技术已经愈发成熟,被越来越多的企业所关注和 ...
- PHP XXE漏洞
PHP xml 外部实体注入漏洞(XXE) 1.环境 PHP 7.0.30Libxml 2.8.0Libxml2.9.0 以后 ,默认不解析外部实体,对于PHP版本不影响XXE的利用 2.原理介绍 X ...