AngularJS: Dynamically loading directives
http://www.codelord.net/2015/05/19/angularjs-dynamically-loading-directives/
----------------------------------------------
AngularJS: Dynamically loading directives
May 19th, 2015 | Comments
It’s hard to write a webapp today without some sort of dynamic feed/list: Facebook’s news feed has photos, text statuses, ads, Twitter’s feed has promoted tweets, image tweets, retweets, and maybe you have a chat/messaging feed in your app with text, videos, photos and stickers.
While this is relatively common, it might not be straightforward to do so in Angular, or what is the Right Way™ for doing this.
The problem
Say that we get from a REST API a list of feed items, that look somewhat like this:
|
Naively, we might say what we really want is to create 2 directives, one for rendering text items (text-feed-item) and one for images (image-feed-item), and write something that looks like this:
|
Of course, this isn’t valid Angular code. So what should you do?
Keep it simple, stupid!
One of my main rules of thumb is to keep away from complexity as much as I can and be explicit. This means that if I have only a handful of different item directives to choose from, I’ll write something very explicit, like this:
|
This has the several advantages:
- Simple as can be
- Explicit
- Easily searchable (say, if you want to find who uses the
image-feed-itemdirective you can use plain search and find this)
But, in case you have more than a handful of different feed item types this might get out of hand or just plain get annoying.
$compile
Angular’s way of dynamically adding directives to the DOM is compiling them. I know the word “compile” feels quite odd in our little corner of web development, but for some reason that’s the word they chose for the process of having Angular parse a DOM node and executing all the Angular goodness it requires.
Making a dynamic directive that does basically what our first naive attempt looked like isn’t that hard, once you know about Angular’s $compile service:
|
|
This will result in something that looks like this if you inspect the DOM:
|
As you can see, $compile has two steps. First, we call it with the HTML we want to generate, which returns a function. We then call that function with the specific scope we want the generated element to have and then we actually get the new element that we can add to the DOM.
Yes, this is more complicated, requires being more comfortable with how Angular works and doesn’t have the benefits I listed above for the simpler solution, but sometimes this approach is necessary.
AngularJS: Dynamically loading directives的更多相关文章
- Dynamically loading an external JavaScript or CSS file
原文: Dynamically loading an external JavaScript or CSS file 通过javascript动态加载css文件和javascript文件,主要是通 ...
- [AngularJS] Lazy loading Angular modules with ocLazyLoad
With the ocLazyLoad you can load AngularJS modules on demand. This is very handy for runtime loading ...
- [AngularJS + Webpack] require directives
direictives/index.js: module.exports = function(ngModule) { //register all the directives here requi ...
- AngularJS 指令(Directives)实践指南
指令(Directives)是所有AngularJS应用最重要的部分.尽管AngularJS已经提供了非常丰富的指令,但还是经常需要创建应用特定的指令.这篇教程会为你讲述如何自定义指令,以及介绍如何在 ...
- AngularJs中的directives(指令part1)
一.指令的职责 指令的职责是修改DOM结构,并将作用域和DOM连接起来.即指令既要操作DOM,将作用域内的数据绑定到DOM节点上,又要为DOM绑定事件调用作用域内的对应的方法. 二.创建自定义指令 ...
- AngularJS自定义指令(Directives)在IE8下的一个坑
在项目中,由于要兼容到IE8,我使用1.2.8版本的angularJS.这个版本是支持自定义指令的.我打算使用自定义指令将顶部的header从其他页面分离.也就是实现在需要header的页面只用在&l ...
- [AngularJS] Lazy Loading modules with ui-router and ocLazyLoad
We've looked at lazy loading with ocLazyLoad previously, but what if we are using ui-router and want ...
- Dynamically loading unmanaged OCX in C#
You'll have to perform a number of steps that are normally taken of automatically when you use the t ...
- AngularJS之directive
AngularJS之directive AngularJS是什么就不多舌了,这里简单介绍下directive.内容基本上是读书笔记,所以如果你看过<AngularJS up and runnin ...
随机推荐
- h5 Visibility API总结
最近活动中的小游戏,有涉及页面隐藏或app后台运行时候,暂停游戏的功能,使用了h5的Visibility API,在此总结如下: 两个属性 document.hidden (Read only) 如果 ...
- 电阻 (resistance)
电阻 (resistance) 题目描述 每次小x物理作业没做完时,总是会去和老师交流感情,他们之间由此建立起来良好的师生关系.于是有一天,老师带着一道物理难题来见小x. 这道题给出了一个有n个电阻的 ...
- Java面试题之CyclicBarrier和CountDownLatch的区别
1.CyclicBarrier的某个线程运行到某个点后停止运行,直到所有线程都达到同一个点,所有线程才会重新运行: CountDownLatch线程运行到某个点后,计数值-1,该线程继续运行,直到计数 ...
- ie下,jquery为动态添加的节点添加事件,用live
jQuery向动态生成的内容添加事件响应 jQuery live() 方法详解 [收藏] 发布时间:2013-07-24 点击次数:176 来源:www.daimajiayuan.com jQuery ...
- tcpreplay 缓存算法研究
一. 缓存算法 1.1 算法目的 流量拆分算法的运算会明显影响包的发送速率,为了提高发送速率, tcpreplay 使用了缓存机制,该部分代码也封装在tcpprep工具里,运行 tcpprep ( ...
- C语言高级应用---操作linux下V4L2摄像头应用程序【转】
转自:http://blog.csdn.net/morixinguan/article/details/51001713 版权声明:本文为博主原创文章,如有需要,请注明转载地址:http://blog ...
- java实现MQ消息收发两种方式
定义: 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过写和检索出入列队的针对应用程序的数据(消息)来通信,而无需专用连接来链接它们.简单理解:蓝牙配对 jar包依赖: <!-- ...
- nginx 根据POST GET方法跳转
location ~ /server/ { proxy_pass_header Server; proxy_set_header Host $http_host; proxy_r ...
- sysbench(mysql测试工具 )
目录 一.基准测试简介 1.什么是基准测试 2.基准测试的作用 3.基准测试的指标 4.基准测试的分类 二.sysbench 1.sysbench简介 2.sysbench安装 3.sysbench语 ...
- sersync+rsync作实时同事
http://liubao0312.blog.51cto.com/2213529/1677586 配置搞定,参照上面的文章,用时搞一搞就OK. 注意IPTABLES的配置及环境变量 最简陋配置: rs ...