[译]angularjs directive design made easy
原文: http://seanhess.github.io/2013/10/14/angularjs-directive-design.html
AngularJS directives很酷
AngularJS一个非常好的特色就是可以创建directives, 或者说是可重复利用的web components. 用了directive你可以创建新的HTML标签和属性.
Directive设计原则
如果你想展示一个user列表, 你可以创建一个directive,这个directive读取$scope.users,并且打印出来:
<user-list/>
和ng-repeat比较, ng-repeat只处理重复. user-list和ng-repeat哪一个可以在多个地方使用呢? 如果你需要在两处不同的地方以不同的形式展示user列表呢?
一个好的directive只做一件事情
ng-repeat比user-list好, 因为它只做一件事情: 它只重复部件, 所以你可以在很多环境下重用他.不要让一个directive去处理所有的事情.
一个好的directive不是为某一个应用特定的
通常来说如果你的directive可以在一个完全不同的应用中使用,那么你这个指令应该设计得不错.
如果展示bindings
第一个要学的是directive如何处理binding: 使用一对花括号. 例如, 写一个directive展示一张照片和一段说明.
directive设计的第一步就是选择一个好的属性的名字. 我用photo-src表示image的src, caption作为文本. 小心不要使用其他directive使用过的名字, 比如ng-src 除非你了解他们是怎么工作的.
第二步, 决定支持attributes、classname或element. 在这个例子中我们使用phone作为一个element.
<photo photo-src="{{photo.url}}" caption="Taken on: {{photo.date}}"/>
注意到了没, 我并没有把整个photo对象给到这个directive. 这是一个好的设计, 这样这个directive可以和不同的数据结构数据对象一起工作.
使用 attrs.$observe读取binding. 当binding改变后它会调用你的callback.
app.directive('photo', function() {
return {
// 指定作为element使用
restrict: 'E',
// 用下面的html替代<photo>
template: '<figure><img/><figcaption/></figure>',
replace: true,
// 观察、操作DOM
link: function($scope, element, attrs) {
attrs.$observe('caption', function(value) {
element.find('figcaption').text(value)
})
// 属性名camel case
attrs.$observe('photoSrc', function(value) {
element.find('img').attr('src', value)
})
}
}
}
})
如果你的component有自己的template, 你可以在一个isolate scope中处理上面所有的attrs.$observe.
app.directive('photo', function() {
return {
restrict: 'E',
templateUrl: 'photo.html',
replace: true,
// pass these two names from attrs into the template scope
scope: {
caption: '@',
photoSrc: '@'
}
}
})
<!-- photo.html -->
<figure>
<img ng-src="{{photoSrc}}"/>
<figcaption>{{caption}}</figcaption>
</figure>
如何读写数据
有些directive需要写数据, 比如ng-model.
现在写一个button toggle指令. 这个指令根据scope上面的一些布尔值设置toggle状态, 当被点击这个值发生改变.
这里不使用大括号,而是使用表达式.
<!-- 这里不使用大括号no double curly braces here -->
<button toggle="preferences.showDetails">Show Details</button>
首先我们在scope:里面使用=, 这个设置是为了使得可以在我们的directive中使用scope.toggle.
app.directive('toggle', function() {
return {
scope: {
toggle: '=',
},
link: function($scope, element, attrs) {
下一步我们使用scope.$watch, 当expression改变的时候调用callback. 当toggle改变的时候我们会添加或者删除active类.
$scope.$watch("toggle", function(value) {
element.toggleClass('active', value)
})
最后, 监听jQuery click事件,更新scope. 我们需要使用$scope.$apply 相应改变.
element.click(function() {
$scope.$apply(function() {
$scope.toggle = !$scope.toggle
})
})
}
}
})
如何暴露events
有时你想让一个controller响应一个directive里面的事件, 比如ng-click. 创建一个scroll directive, 当用户scroll一个element的使用会调用一个function.
<textarea scroll="onScroll(offset)">...</textarea>
类似于toggle button, 我们映射scroll属性中的function到我们指令的scope.
app.directive('scroll', function() {
return {
scope: {
scroll: "&"
},
link: function($scope, element, attrs) {
我们依然使用scope.$apply
element.scroll(function() {
$scope.apply(function() {
var offset = element.scrollTop()
$scope.scroll({offset:offset})
})
})
}
}
})
如何包含HTML内容
Directive默认可以有html内容.
写一个modal组件: 一个有关闭按钮的弹出窗口
<modal>
<p>Some contents</p>
<p>Put whatever you want in here</p>
</modal>
modal不止一个元素. 我们写一个template html, 我们在一个div中使用了一个特别的ng-transclude指令 他用来获取得到modal中的html内容.
<div class="modal">
<header>
<button>Close</button>
<h2>Modal</h2>
</header>
<div class="body" ng-transclude></div>
</div>
注意要设置 transclude: true:
app.directive('modal', function() {
return {
restrict: 'E',
templateUrl: 'modal.html',
replace: true,
transclude: true,
}
})
[译]angularjs directive design made easy的更多相关文章
- 学习AngularJs:Directive指令用法(完整版)
这篇文章主要学习AngularJs:Directive指令用法,内容很全面,感兴趣的小伙伴们可以参考一下 本教程使用AngularJs版本:1.5.3 AngularJs GitHub: http ...
- 学习AngularJs:Directive指令用法
跟我学AngularJs:Directive指令用法解读(上) http://blog.csdn.net/evankaka/article/details/51232895 跟我学AngularJs: ...
- angularjs directive 实例 详解
前面提到了angularjs的factory,service,provider,这个可以理解成php的model,这种model是不带html的,今天所说的directive,也可以理解成php的mo ...
- Angularjs directive全面解读(1.4.5)
说到Angularjs directive即指令,可以这么说Angularjs的灵魂就是指令,学会Angularjs指令那么你的Angularjs的武功就修炼了一半了,当然这只是鄙人的一点点独到见解, ...
- [译]AngularJS 1.3.0 开发者指南(一) -- 介绍
[译]AngularJS 1.3.0 开发者指南(一) -- 介绍 Angular是什么 ? AngularJS是一款针对动态web应用的结构框架. 它可以让像使用模板语言使用HTML, 并且可以扩展 ...
- angularjs directive (自定义标签解析)
angularjs directive (自定义标签解析) 定义tpl <!-- 注意要有根标签 --> <div class="list list-inset" ...
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- [译]AngularJS 1.3.0 开发者指南(一) -- 介绍 (转)
http://www.cnblogs.com/lzj0616/p/6440563.html [译]AngularJS 1.3.0 开发者指南(一) -- 介绍 Angular是什么 ? Angular ...
- 50.AngularJs directive详解及示例代码
转自:https://www.cnblogs.com/best/tag/Angular/ 本教程使用AngularJs版本:1.5.3 AngularJs GitHub: https://github ...
随机推荐
- django 提示ImportError: cannot import name json_response
from json_response import JsonResponse, json_response as json_resp 使用的语句如上,其实并不是没有安装,只是需要升级一下 pip in ...
- JSP+Servlet+javabean+oracle实现页面多条件模糊查询
之前写过一篇JSP+Servlet+javabean+mysql实现页面多条件模糊查询 使用的是mysql进行的分页查询,mysql用limit控制,而oracle则是用rownum,今天第一次写or ...
- PHP面向对象之朝花夕拾
虽然平时都在用面向对象的思维编程.但是都是基于框架,很多技术细节一段时间不用就会逐渐模糊.拾遗部分:面向对象的三大特称:继承多态封装. 构造方法.__construct() 构造方法.再new的时候 ...
- (转)雅虎工程师提供的css初始化示例代码
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,textarea,p,b ...
- Python ValueError: IO operation on closed file
ValueError IO operation on closed file表示处理了已经被关闭的数据,在python 中 with语句的上下文会帮助处理,也就是说,当python的处理代码不对齐的时 ...
- angular ui-router 缓存问题
在个别情况下 $state.go()路径和参数完全相同的时候页面因为缓存问题可以直接跳转,但是不能重新获取数据 通过路由参数可以解决 路由 .state('app.***.***', { url: ' ...
- UML图例
概述 常用建模方法:BOOCH.OMT.OOSE等 统一建模语言:UML(Unified Modeling Language) 软件开发过程:RUP(Rational Unified Process) ...
- Android之Proguard语法
-include {filename} 从给定的文件中读取配置参数 -basedirectory {directoryname} 指定基础目录为以后相对的档案名称 -injars {class_pat ...
- 利用WireShark进行DNS协议分析
一.准备工作 系统是Windows 8.1Pro 分析工具是WireShark1.10.8 Stable Version 使用系统Ping命令发送ICMP报文. 二.开始工作 打开CMD.exe键入: ...
- vs2013安装闪退及vs2010 vs2013打开时提示 未能完成的操作 及vs2013安装时出现图片后闪退
vs2013打开时提示如上图,vs2010只有 未能完成的操作 这样的提示. 这时.net 4.0开发的程序打开也毫无反应,应该是.net framework出了问题.查看控制面板-卸载程序,发现 ...