一句话: 直接return link函数可以解决大多数问题,无须死扣用法

1. 上源码 (dom操作,事件,css,mode操作全包括了)

 <h3>Custom directive, with dom operation, events, css and scope model operation</h3> 

         <div ng-controller="DateController">
Date format: <input ng-model="format"> <hr/>
Current time is: <span my-current-time="format"></span>
</div> <script>
var myApp = angular.module('myApp', []); myApp.controller('DateController', function($scope) {
$scope.format = "M/d/yy h:mm:ss a";
}); myApp.directive('myCurrentTime', ['$interval', 'dateFilter', function($interval, dateFilter) {
function link(scope, element, attrs) {
var format, timeoutId; function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
} function updateTime() {
element.text(dateFilter(new Date(), format));
element.css({'background-color': getRandomColor()});
} scope.$watch(attrs.myCurrentTime, function(value) {
format = value;
updateTime();
}); element.on('$destroy', function() {
$interval.cancel(timeoutId);
}); element.on('click', function(){
alert('Date format is changing to yyyy/MM/dd hh');
scope.format = "yyyy/MM/dd hh " + getRandomColor();
}); // start the UI update process; save the timeoutId for canceling
timeoutId = $interval(function() {
updateTime(); // update DOM
}, 1000);
} return {
link: link
};
}]);
</script>
</div>

2. 在线查看运行效果

http://jimuyouyou.github.io/angular-bootstrap-rest-seed/examples/angular/6-custom-directive.html

3. 项目地址

github: https://github.com/jimuyouyou/angular-bootstrap-rest-seed

简话Angular 06 Angular自定义指令的更多相关文章

  1. Angular.js之自定义指令学习笔记

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  2. angular中关于自定义指令——repeat渲染完成后执行动作

    业务中有时需要在异步获取数据并用ng-repeat遍历渲染完页面后执行某个操作,angular本身并没有提供监听ng-repeat渲染完成的指令,所以需要自己动手写.有经验的同学都应该知道,在ng-r ...

  3. angular.js创建自定义指令-demo3

    html: <!doctype html><html ng-app="myModule"> <head> <meta charset=&q ...

  4. Angular 学习笔记——自定义指令之间的交互

    <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <met ...

  5. Angular 学习笔记——自定义指令

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  6. angular 自定义指令 directive transclude 理解

    项目中断断续续的用了下angular,也没狠下心 认真的学习.angular 特别是自定义指令这块 空白. transclude 定义是否将当前元素的内容转移到模板中.看解释有点抽象. 看解释有点抽象 ...

  7. 利用angularJs自定义指令(directive)实现在页面某一部分内滑块随着滚动条上下滑动

    最近老大让我一个效果实现在页面某一部分内滑块随着滚动条上下滑动,说明一下我们项目使用技术angularJs.大家都知道,使用jquery很好实现. 那么angular如何实现呢,我用的是自定义指令(d ...

  8. angularJs自定义指令(directive)实现滑块滑动

    最近老大让我一个效果实现在页面某一部分内滑块随着滚动条上下滑动,说明一下我们项目使用技术angularJs.大家都知道,使用jquery很好实现. 那么angular如何实现呢,我用的是自定义指令(d ...

  9. 简话Angular 01 初识Angular 数据绑定

    1. Angular有哪些突出优点 1) MVC 基于Html-Javascript 2) 依赖注入 3) 数据双向绑定,响应式页面设计 4) 模块化,自定义指令 2. 简话数据绑定 1) 代码: & ...

随机推荐

  1. C# MVC框架初学者

    推荐网站:http://blog.csdn.net/zhuyu19911016520/article/category/6318590

  2. Git 基础 —— 安装 配置 别名 对象

    Git 基础学习系列 Git 基础 -- 安装 配置 别名 对象 Git 基础 -- 常用命令 Git 基础 -- 常见使用场景 Git基础 -- Github 的使用 Git 安装 Git下载地址 ...

  3. js媒体查询设置根字号

    !function(n){var e=n.document,t=e.documentElement,i=750,d=i/50,o="orientationchange"in n?& ...

  4. C#学习笔记(十三):继承

    继承 object是引用类型 public:最高权限,公开的 Protected:外部不可以访问 Internal:类的默认访问是什么作用域 Private:类成员默认   基类实例:可以通过base ...

  5. C#学习笔记(一):C#简介

    计算机语言计算机语言是指用于人与计算机之间通讯的语言机器码——汇编语言——高级语言(面向过程(线性).面向对象(类).面向组件(Unity)) 一.计算机语言发展趋势1.简单:代码生成逻辑2.面向人类 ...

  6. [NLP/Attention]关于attention机制在nlp中的应用总结

    原文链接: https://blog.csdn.net/qq_41058526/article/details/80578932 attention 总结 参考:注意力机制(Attention Mec ...

  7. FPGA 概述

    概述 verilog HDL Verilog HDL基本结构 1 Verilog HDL程序是由模块构成的.每个模块嵌套在module和endmodule声明语句中. 2 每个Verilog HDL源 ...

  8. U盘中病毒了怎么办

    声明: 该方案仅是我处理自己U盘的心得,如果您看到这篇文件,并使用此方法,有任何数据丢失或者损失,我概不负责 问题: U盘里面显示占用了空间,就是看不到文件,或者仅有一两个自己不认识的文件 处理方案: ...

  9. resource not found : rosbridge_server

    1.放到src下,改名字为rosbridge_server,编译catkin_make git clone https://github.com/RobotWebTools/rosbridge_sui ...

  10. Mysql tinyint长度为1时在java中被转化成boolean型

    MySql 中的tinyint(1)的使用 在MySql中如何定义像Java中类型的Boolean类型数据..其实,mysql中 是没有直接定义成Boolean这种数据类型. 它只能定义成 tinyi ...