之前写过一篇了 http://www.cnblogs.com/keatkeat/p/3903673.html

但某些部分写的不太清楚,甚至有点错误,所以今天特地在这里再来谈谈。

这篇主要是说指令的隔离还有和controller的沟通.

指令的运行过程基本上我们可以简单的理解为 : template -> compile -> controller -> pre-link -> post-link

我们通常只是用到post link,但如果你的指令有嵌套,子层需要父层的一些值的话,可以考虑把这些初始值写在 controller 或者是 pre-link

pre-link 是上层到下层调用, post-link 则相反下至上调用.

在开发中当一个内容经常重复时,我们就会把它封装成指令。

这种组件的思想可以节省很多开发时间。

而封装的指令不太可能每一次使用都是一模一样的。

所以我们必须可以调整它。

一般上我们使用 attr.  (就好象函数和参数的感念)

<div ng-controller="ctrl">
<dir data-obj="obj" data-string="{{string}}" data-method="method(outsideParam,insideParam)" ></dir>
</div> <script>
var app = angular.module("app", []);
app.directive("dir", ["$parse",function ($parse) {
return {
restrict: "E",
template: '<div>' +
'{{ obj.value }}' +
'{{ string }}' +
'<div ng-click="click()"></div>' +
'</div>',
link: function (scope, elem, attr) {
//scope.$parent 是controller scope, 隔离只是表示没有继承,但是parent还是可以访问得到。
scope.click = function () {
scope.method({ insideParam: "88" }); //这里你可以选择要不要覆盖掉外面的 outsideParam. 非常灵活
}
},
scope: {
obj: "=", //双向绑定,内部变化会在digest的时候同步去外面
//obj: "=object", //data-object="obj" 如果你的属性名字很不一样的话,angular 只会把 a-b-c 换成 驼峰式
//obj : "=?object", //加 ? 表示这个属性不一定会有。如果没写 ? 你使用 "=" 但是写属性的话是会报错的哦
string: "@", //单向绑定 (只限于string,int, object array can't), 内部改变不会影响外面
method : "&" //绑定方法, 注意param, 它是可以像 bind 那样,放入不同阶层的 params
}
}
}])
app.controller("ctrl", ["$scope", "$timeout", function ($scope, $timeout) {
$scope.obj = {
value: "lu xiao feng"
}
$scope.string = "luxiaofeng";
$scope.outsideParam = "77";
$scope.method = function (outsideParam, insideParam) {
log(outsideParam); //77 这个是预先设定的
log(insideParam); //
}
}]);
</script>

更新 : 2015-07-01

transclude

参考 : https://docs.angularjs.org/api/ng/service/$compile  (里面的 transclusion)

在做指令的时候,我们需要很多动态的东西,像之前的属性,我们可以通过 scope : { "=","@","&" } 来实现动态。

那么如果我们想实现模板或者说某部分的element动态的话,我们可以使用 translude

<div ng-controller="ctrl">
<dir>
<span>{{ name }}</span>
</dir>
</div>
<script>
var app = angular.module("app", []);
app.controller("ctrl", ["$scope", "$timeout", function ($scope, $timeout) {
$scope.name = "keatkeat";
}]);
app.directive("dir", [function () {
return {
restrict: "E",
transclude: true,
template: '<div>' +
'<div ng-transclude></div>' +
'</div>',
link: function (scope, elem, attr, ctrl, $transclude) { },
scope: {}
}
}]);
</script>

只要在指令注册时写上 translude : true 就可以了。

ng 会把指令中间的 element keep 起来, 然后放到 ng-transclude 里头 .

ng-transclude 是 angular 写好的一个指令很方便使用。

如果你想发挥到淋漓精致的话,有几点你需要搞清楚!

compile : function (elem,attr,$transclude) | link : function (scope,elem,attr,ctrl,$transclude)

controller : ["$transclude",function($transclude){}]

compile,link,controller 都可以获取到 $transclude 方法。

这个方法其实就是 linkFn , ng 预先把指令内的elem 拿去$compile 了

$transclude(scope,cloneAttachFn)

一般上这个 $tranclude 是这样用的

$transclude(function (clone, scope) {
log("trs : " + scope.$id); <- 自己是一个新的scope
log(scope.$parent); <- 指令的scope ,但却继承了 ctrl 的 scope 哦 ! elem.empty(); elem.append(clone); 最后就append 这个clone 出去就行了
});

很重要 : 如果没有指定 scope 的话,scope 是一个新的scope, 它继承了ctrl , 但它是指令的孩子。 这有点难理解,但是好处很多 ^^ 下面是原文的解释

When you call a transclude function it returns a DOM fragment that is pre-bound to a transclusion scope. This scope is special, in that it is a child of the directive's scope (and so gets destroyed when the directive's scope gets destroyed) but it inherits the properties of the scope from which it was taken.

当父指令有 transclude 时,子指令也可以调用的到, 这也是 ng-transclude 能实现的原因了。

更新 : 2016-03-01

angular 1.5.0 版本 translude 有了多一个 mutilple 的功能

参考 : http://blog.thoughtram.io/angular/2015/11/16/multiple-transclusion-and-named-slots.html

<product>
@*<detail-one>
<div>detail one</div>
</detail-one>*@
<detail-two>
<div>detail two</div>
</detail-two>
</product>
app.directive("product", [function () {
return {
restrict: "E",
transclude: {
//note :
//这对象是一个对接对象, key 对应内部调用 , value 对应外部 (这个算是angular规范,不只是transclude有这样的设计)
detailOne: "?detailOne", // ? 代表可有可无
detailTwo: "detailTwo"
},
template: [
'<div>',
'<div>',
//如果外部没有传进来,会使用default的做显示,有传进来的话则会覆盖掉default
'<div ng-transclude="detailOne" >default value for details one</div>',
'</div>',
'<br/>',
'<div>',
'<div ng-transclude="detailTwo" >default value for details one</div>',
'</div>',
'</div>',
].join(''),
link: function (scope, elem, attr) {}
}
}]);

更新 : 2016-03-01

1.5.0 版本 angular 多了一个 component 组件

refer :

https://docs.angularjs.org/guide/component

https://toddmotto.com/exploring-the-angular-1-5-component-method/

文章开头就说明了 a Component is a special kind of directive, 组件是一种特别的指令 (意思是指令基本上可以完成所有组件的功能)

那组件主要的好处就是它比较简单理解和使用, 迈向 angular 2.0 你应该尽可能的使用组件 (当然如果组件不满足需求,指令依然是需要的)

这里我主要是讲概念的东西。

组件主要就是简化了以前指令的复杂性。所以你会发现他移除了很多我们不常用到的东西。

比如 : compile, link, scope, multiElement, priority, restrict 都移除了。

而且scope直接就是 isolate 的

所以当我们在使用组件时,我们也必须延着这种简单的思路去设计。不要再去使用$scope继承等等。

组件和外部沟通使用的是 bindings , 这个等价于 bindToController + scope : { @,&,= }

1.5.0 多了一个 "<" 符号,叫单向绑定

我们知道以前我们要传 string,int 我们用 "@"

我们要传对象,要双向,我们用 "="

但是我们要传对象,但是不要双向,我们就没有的用了,而现在可以使用 "<"

这里给大家复习一下 :

@ 外面可以改变里面,里面不可以改变外面 (only for 值类型 e.g. string,int)
= 里面外面完全同步 (如果是obj,array的话,在某些情况下是会打断引用的,但即使打断他还是能同步key,value)
< 和 = 一样,区别在里面不会同步去外面了。(当然如果obj引用一样的话,你只改prop还是会影响到外面啦)

组件的require和指令也有点不一样 (多了一个叫 $onInit 的东东)

app.component("product", {
template: [
'<div>',
'<color></color>',
'</div>'
].join(''),
controller: ["$scope", "$timeout", function ($scope, $timeout) {
this.shareMethod = function () {
console.log("share method");
}
}]
});
app.component("color", {
template: [
'<div>color</div>'
].join(''),
require : {
productCtrl : "^product"
},
controller: ["$scope", function ($scope) {
this.$onInit = function () {
this.productCtrl.shareMethod();
}
}]
});

require 向上寻找 component 然后在 $onInit 的时候才会注入到ctrl内,require 对象也是一个mapping对象.

angularjs directive and component 指令与组件 ( 1.5.0 以后 )的更多相关文章

  1. AngularJS:directive自定义的指令

    除了 AngularJS 内置的指令外,我们还可以创建自定义指令. 你可以使用 .directive 函数来添加自定义的指令. 要调用自定义指令,HTML 元素上需要添加自定义指令名. 使用驼峰法来命 ...

  2. 学习AngularJs:Directive指令用法(完整版)

    这篇文章主要学习AngularJs:Directive指令用法,内容很全面,感兴趣的小伙伴们可以参考一下   本教程使用AngularJs版本:1.5.3 AngularJs GitHub: http ...

  3. 学习AngularJs:Directive指令用法

    跟我学AngularJs:Directive指令用法解读(上) http://blog.csdn.net/evankaka/article/details/51232895 跟我学AngularJs: ...

  4. angularjs directive学习心得

    一些常见的错误 在angularjs里,创建directive时,directive的名称应该要使用驼峰式,例如myDirective,而在html里要调用它的时候,就不能用驼峰式了,可以用my-di ...

  5. 带你走近AngularJS - 创建自己定义指令

    带你走近AngularJS系列: 带你走近AngularJS - 基本功能介绍 带你走近AngularJS - 体验指令实例 带你走近AngularJS - 创建自己定义指令 ------------ ...

  6. 带你走近AngularJS 之创建自定义指令

    带你走近AngularJS 之创建自定义指令 为什么使用AngularJS 指令? 使用过 AngularJS 的朋友应该最感兴趣的是它的指令.现今市场上的前端框架也只有AngularJS 拥有自定义 ...

  7. Angularjs directive全面解读(1.4.5)

    说到Angularjs directive即指令,可以这么说Angularjs的灵魂就是指令,学会Angularjs指令那么你的Angularjs的武功就修炼了一半了,当然这只是鄙人的一点点独到见解, ...

  8. angular5 directive和component的区别

    指令分为三类,组件,属性指令和结构性指令 组件(Component directive):UI组件,继承于Directive: 属性指令(Attribute directive):改变组件的样式: 结 ...

  9. 基于AngularJS的个推前端云组件探秘

    基于AngularJS的个推前端云组件探秘 AngularJS是google设计和开发的一套前端开发框架,帮助开发人员简化前端开发的负担.AngularJS将帮助标准化的开发web应用结构并且提供了针 ...

随机推荐

  1. [原创]python MySQLdb在windows环境下的安装、出错问题以及解决办法

    版权声明:本文为博主原创文章,未经博主允许不得转载. 问题:windows下安装MySQLdb的方法 解析:python没有php那种集成环境,比如wamp那种集成软件直接把所有需要的东西全部一次性搭 ...

  2. jquery动态添加元素无法触发绑定事件的解决方案。

    jquery动态添加元素无法触发绑定的事件的解决方案. ╭(●`∀´●)╯二狗最近在工作中遇到一个问题,即当用jquery动态添加元素后,发现给动态添加的元素却无法触发事件(╯#-_-)╯╧═╧ ( ...

  3. 站内信DB设计实现

    两年前,万仓一黍在博客园发了两篇关于站内信的设计实现博文,<群发"站内信"的实现>.<群发"站内信"的实现(续)>,其中阐述了他关于站内 ...

  4. RMAN-00554: initialization of internal recovery manager package failed RMAN-04005

    [oracle@rac11g1 ~]$ rman target haha/haha@rac11g Recovery Manager: Release 11.2.0.3.0 - Production o ...

  5. JavaScript 总结

    1. JavaScript prototype属性是一个对象 当一个函数在定义之后 就会自动获得这个属性.其初始值是一个空对象.新建了一个名为Cat的构造函数,其prototype为一个对象,cons ...

  6. HTML中属性ID和属性NAME的区别(转)

    ID和Name都可以用来标识一个标记,Javascript分别有两个方法getElementById和getElementByName来定位Dom节点. 区别如下: 1.我们知道在网页做Post提交时 ...

  7. URI、URL、URN

    URI.URL.URN URI(Uniform Resource Identifie):统一资源标识符 URL(Uniform Resource Locator):统一资源定位符 URN(Unifor ...

  8. oracle登陆连接的问题

    一.登陆 1.使用客户端 直接在database中配置: IP:1521/orcl 其中IP为要连接的IP 其中1521为要连接的数据库的端口 其中orcl为要连接的数据库的实例名字 2.使用命令行 ...

  9. libthrift0.9.0解析(三)之TProtocol&TTransport

       以上是transport和protocol的类图和结构图. transport封装了底层的传输通道,主要有read/write.open/close等基本的读写方法,而且都是对于二进制数据. p ...

  10. 重新认识Swift中的可选型(Swift2.1)

    //: Playground - noun: a place where people can play import UIKit /* Swift中nil代表是是另外一种类型, 而不像OC那样, 任 ...