[AngularJS] Hijacking Existing HTML Attributes with Angular Directives
Angular overrides quite a few existing HTML elements and attributes. This can be a useful technique in our own applications. We will build a directive that adds additional functionality to the src property of an <img>.
Javascript:
/**
* Created by Answer1215 on 12/8/2014.
*/
angular.module('app', []).directive('src', function () {
var URL_RE = /^http:\/\/[^\/]*/;
var HTTP_RE = /^(http|https):\/\//; return function (scope, element, attrs) {
var context = {url: attrs.src.match(URL_RE)[0]};
context.domain = context.url.replace(HTTP_RE, '');
/*
* Object {url: "http://fursealworld.com", domain: "fursealworld.com"} app.js:11
Object {url: "http://resources.news.com.au", domain: "resources.news.com.au"} app.js:11
Object {url: "http://www.hdwallpaperscool.com", domain: "www.hdwallpaperscool.com"}
* */
var templateFn = _.template('<a href="<%= url %>" target="_blank">Photo courtesy of <%= domain %></a>');
element.css({border: "2px solid grey"});
element.after(templateFn(context));
};
});
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hijacking HTML Attributes</title>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/css/bootstrap.css">
<link rel="stylesheet" href="./main.css">
</head>
<body> <div class="container-fluid" ng-app="app">
<div class="row">
<div class="col-xs-4">
<img src="http://fursealworld.com/wp-content/uploads/2013/03/1280BabyHarpSeal11.jpg"/>
</div>
<div class="col-xs-4">
<img src="http://resources.news.com.au/files/2012/01/13/1226243/386315-harp-seal-1.jpg"/>
</div>
<div class="col-xs-4">
<img src="http://www.hdwallpaperscool.com/wp-content/uploads/2014/10/baby-seal-widescreen-wallpaper-for-background-free.jpg"/>
</div>
</div>
</div> <script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular.js"></script>
<script src="app.js"></script>
</body>
</html>

[AngularJS] Hijacking Existing HTML Attributes with Angular Directives的更多相关文章
- [AngularJS] Consistency between ui-router states and Angular directives
ui-router's states and AngularJS directives have much in common. Let's explores the similarities bet ...
- [AngularJS] Using Services in Angular Directives
Directives have dependencies too, and you can use dependency injection to provide services for your ...
- AngularJS源码解析1:angular自启动过程
angularJS加载进来后,会有一个立即执行函数调用,在源代码的最下面是angular初始化的地方.代码展示: bindJQuery(); publishExternalAPI(angular); ...
- [RxJS + AngularJS] Sync Requests with RxJS and Angular
When you implement a search bar, the user can make several different queries in a row. With a Promis ...
- [AngularJS 2 实践 一]My First Angular App
最近一直在看关于AngularJS 2的资料,查看了网上和官网很多资料,接下来就根据官网教程步骤一步步搭建我的第一个Angular App AngularJS 2说明请参考:http://cnodej ...
- AngularJS进阶(二十五)requirejs + angular + angular-route 浅谈HTML5单页面架构
requirejs + angular + angular-route 浅谈HTML5单页面架构 众所周知,现在移动Webapp越来越多,例如天猫.京东.国美这些都是很好的例子.而在Webapp中,又 ...
- angularjs探秘<三> 控制器controller及angular项目结构
先来看一个例子 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=&quo ...
- angularjs中使用锚点,angular路由导致锚点失效的两种解决方案
壹 ❀ 引 公司新项目开发中,首页要做个楼层导航效果(如下图),要求能点击图标对应跳到楼层即可,因为不需要跳转过度动画,也要求最好别用JQ,想着原生js操作dom计算top的兼容性,想着用锚点实现算 ...
- 一个全栈Web/移动App开发学习路径
HTML.CSS 和 JavaScript 网页开发的基本元素,包括HTML.CSS个JavaScript.本课程完全适合零基础的同学,当然如果你有相关开发经验更好.在课程开始呢,我们先探索与讨论HT ...
随机推荐
- Scrum之Sprint物件
产品订单(Product Backlog) 一个需求的列表. 一般情况使用用户故事来表示backlog条目 理想情况每个需求项都对产品的客户或用户有价值 Backlog条目按照商业价值排列优先级 优先 ...
- mysql根据时间查询前一天数据
MySql数据库如何根据时间查询前一天的数据?本文整理了几个解决方法,有需要的朋友参考下. 本节内容:用MySql怎么根据时间查询前一天的数据. 例1: 代码示例: select * from t ...
- 开源的c语言人工神经网络计算库 FANN
这年头机器学习非常的火,神经网络算是机器学习算法中的比较重要的一种.这段时间我也花了些功夫,学了点皮毛,顺便做点学习笔记. 介绍人工神经网络的基本理论的教科书很多.我正在看的是蒋宗礼教授写的<人 ...
- 使用 XMPP 构建一个基于 web 的通知工具——转
Inserting of file(使用 XMPP 构建一个基于 web 的通知工具.docx) failed. Please try again. http://www.ibm.com/develo ...
- FIREDAC FDConnection 连接池 连接串
一.FDConnection 连接池 http://docs.embarcadero.com/products/rad_studio/firedac/frames.html?frmname=topic ...
- 使用sem_post信号量进行线程同步
写了一小段程序,测试一下线程同步的问题,如下: #include <stdio.h> #include <string.h> #include <semaphore.h& ...
- 杭电ACM减花布条
这是原题的地址 http://acm.hdu.edu.cn/showproblem.php?pid=2087 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条 ...
- keil编译时出现*** WARNING L2: REFERENCE MADE TO UNRESOLVED EXTERNAL
*** WARNING L2: REFERENCE MADE TO UNRESOLVED EXTERNAL *** WARNING L1: UNRESOLVED EXTERNAL SYMBOL 解决: ...
- JVM启动参数小结
一:JVM启动参数共分为三类: 其一是标准参数(-),所有的JVM实现都必须实现这些参数的功能,而且向后兼容: 其二是非标准参数(-X),指的是JVM底层的一些配置参数, ...
- Oracle中wm_concat()函数的使用
Oracle中wm_concat()函数的使用 wm_concat()函数是oracle行列转换函数,该函数可以把列值以‘,’分割开来,并显示成一行. 1.原数据: 2.把结果分组以‘|’分隔,以一行 ...