之前写过一篇了 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. otl获得sql出错位置(oracle)

    项目的一个需要,要获得sql出错的位置,从而给出类似sqlplus的错误提示(如下) sql出错原因易获得,记录在otl_exception::msg,但出错位置就不是那么容易了. 默认情况下otl异 ...

  2. JQuery的ready函数与JS的onload的区别详解

    JQuery的ready函数与JS的onload的区别:1.执行时间window.onload必须等到页面内包括图片的所有元素加载完毕后才能执行.$(document).ready()是DOM结构绘制 ...

  3. 一般处理程序中使用Session出现未将对象引用设置到对象的实例

    遇到问题:未将对象引用设置到对象的实例 那就在你的一般处理程序中加入红色背景的代码吧 using System; using System.Collections.Generic; using Sys ...

  4. AIX Study之--AIX网卡配置管理(ent0、en0、et0)

    AIX Study之--AIX网卡配置管理(ent0.en0.et0) 1.查看AIX系统网卡信息: [root@aix211 /]#lsdev |grep et  en0 Available 1L- ...

  5. HDU 2639 Bone Collector II(01背包变型)

    此题就是在01背包问题的基础上求所能获得的第K大的价值. 详细做法是加一维去推当前背包容量第0到K个价值,而这些价值则是由dp[j-w[ i ] ][0到k]和dp[ j ][0到k]得到的,事实上就 ...

  6. ubuntu eclipse CDT 问题

    问题一:ubuntu eclipse c++ launch failed binary not found 解决:建完项目后 查看在项目中是不是有debug目录,说明没有编译.仅仅是须要做例如以下操作 ...

  7. [RxJS] Creation operators: empty, never, throw

    This lesson introduces operators empty(), never(), and throw(), which despite being plain and void o ...

  8. atitit。浏览器缓存机制 and 微信浏览器防止缓存的设计 attilax 总结

    atitit.浏览器缓存机制 and 微信浏览器防止缓存的设计 attilax 总结 1. 缓存的一些机制 1 1.1. http 304 1 1.2. 浏览器刷新的处理机制 1 1.3. Expir ...

  9. myqltransactionRollbackexception deadlock found when trying to get lock

    linux 下远程连接mysq 命令: mysql -h "1.0.0.1" -u username -p 1 获 取锁等待情况 可以通过检查 table_locks_waited ...

  10. yii criteria select column as 与 时间段查询

    需要查询某时间段的记录,但是数据库里只有一个时间记录,如果写sql的话,很快的,放到yii里一时竟然没办法... 不过,最后还是解决了,使用了一个第三方的插件 参考http://www.yiifram ...