[译]Exploring Angular 1.3: Binding to Directive Controllers
原文: http://blog.thoughtram.io/angularjs/2015/01/02/exploring-angular-1.3-bindToController.html
Angular1.2引入了新的语法 controllerAs, 它让scope更加清楚、干净, 让controller更加聪明. 在我们的Angular应用中使用controllerAs可以避免开发者经常遇到的一些问题.
controllerAs 做为命名空间
让我们用代码说事,有两个controller如下:
function ControllerOne($scope) {
$scope.foo = 'Pascal';
}
function ControllerTwo($scope) {
$scope.foo = 'Christoph';
}
app.controller('ControllerOne', ControllerOne);
app.controller('ControllerTwo', ControllerTwo);
HTML如下:
<div ng-controller="ControllerOne">
{{foo}}
<div ng-controller="ControllerTwo">
{{foo}}
</div>
</div>
ControllerTwo中的{{foo}}会覆盖ControllerOne中的 {{foo}}, 第一个{{foo}}会显示'Pascal', 第二个{{foo}}显示'Christoph'.
如果我们想在ControllerTow中显示'Pascal'可以使用scope的$parent属性去引用父作用域:
<div ng-controller="ControllerOne">
{{foo}}
<div ng-controller="ControllerTwo">
{{$parent.foo}}
</div>
</div>
然而使用$parent不是一个好的解决方案, 因为如果嵌套一多我们的代码就不利于维护了. 想象下如果你有4层或5层嵌套. 你的代码会变成$parent.$parent.$parent.$parent, 这是不是很糟糕?
首先我们使用this替代$scope:
function ControllerOne() {
this.foo = 'Pascal';
}
function ControllerTwo() {
this.foo = 'Christoph';
}
接下来, 我们修改ngController指令,使用controllerAs语法:
<div ng-controller="ControllerOne as ctrl1">
{{ctrl1.foo}}
<div ng-controller="ControllerTwo as ctrl2">
{{ctrl2.foo}}
</div>
</div>
当使用controller的时候,我们就可以使用controllerAs. 例如在Angular的$routeProvider中我们也可以使用controllerAs.
$routeProvider.when('/', {
templateUrl: 'stateTemplate.html',
controllerAs: 'ctrl',
controller: 'StateController'
});
指令也可以有controller, 所以我们在定义指令的时候也可以使用controllerAs.
app.controller('SomeController', function () {
this.foo = 'bar';
});
app.directive('someDirective', function () {
return {
restrict: 'A',
controller: 'SomeController',
controllerAs: 'ctrl',
template: '{{ctrl.foo}}'
};
});
在指令中使用controllerAs的问题
我们说过,当使用controllerAs的时候controller的scope绑定到controller的this对象, 也就是说this代表我们的scope. 那么当指令有自己隔离的scope时,它会如何工作呢?
下面的指令有一个隔离的scope,一个controller和一个使用controller属性的template:
app.directive('someDirective', function () {
return {
scope: {},
controller: function () {
this.name = 'Pascal'
},
controllerAs: 'ctrl',
template: '<div>{{ctrl.name}}</div>'
};
});
如果这个name要进行双向绑定该怎么做呢?
app.directive('someDirective', function () {
return {
scope: {
name: '='
},
// ...
};
});
如果我们在外部修改隔离scope的属性,他不会反应到controller的this对象中去. 在AngularJS1.2中, 当属性变化的时候我们使用$scope服务显示的重新对我们的scope赋值. 不要忘了把$watch的callback绑定到controller的this:
app.directive('someDirective', function () {
return {
scope: {
name: '='
},
controller: function ($scope) {
this.name = 'Pascal';
$scope.$watch('name', function (newValue) {
this.name = newValue;
}.bind(this));
},
// ...
};
});
等等,开始去掉的$scope 现在又回来了. 而且现在为了进行双向绑定我们额外写了许多代码.
幸运的是, 这些在AngularJS 1.3中得到了很好的解决!
使用bindToController绑定controller
Angular 1.3在指令中引入了一个新的属性bindToController. 当设置bindToController为true时,属性是绑定到controller而不是scope.
这意味, 当controller实例化, 隔离scope的初始值绑定到this上, 将来对这些属性值的改变也会自动反应.
app.directive('someDirective', function () {
return {
scope: {
name: '='
},
controller: function () {
this.name = 'Pascal';
},
controllerAs: 'ctrl',
bindToController: true,
template: '<div>{{ctrl.name}}</div>'
};
});
看, 现在不需要$scope了.
1.4中的改善
在1.4中, bindToController更加强大.在1.4中, 我们可以把所有要绑定到隔离scope的属性移到bindToController中.
下面的例子中. 不再在scope中定义scope属性了:
app.directive('someDirective', function () {
return {
scope: {},
bindToController: {
someObject: '=',
someString: '@',
someExpr: '&'
}
controller: function () {
this.name = 'Pascal';
},
controllerAs: 'ctrl',
template: '<div>{{ctrl.name}}</div>'
};
});
[译]Exploring Angular 1.3: Binding to Directive Controllers的更多相关文章
- [AngularJS - thoughtram] Exploring Angular 1.3: Binding to Directive Controllers
The post we have: http://www.cnblogs.com/Answer1215/p/4185504.html gives a breif introduce about bin ...
- [译] 关于 Angular 依赖注入你需要知道的
如果你之前没有深入了解 Angular 依赖注入系统,那你现在可能认为 Angular 程序内的根注入器包含所有合并的服务提供商,每一个组件都有它自己的注入器,延迟加载模块有它自己的注入器. 但是,仅 ...
- Angular学习(8)- directive
<!DOCTYPE html> <html ng-app="MyApp"> <head> <title>Study 9</ti ...
- angular 自定义指令详解 Directive
在angular中,Directive,自定义指令的学习,可以更好的理解angular指令的原理,当angular的指令不能满足你的需求的时候,嘿嘿,你就可以来看看这篇文章,自定义自己的指令,可以满足 ...
- [Angular 2] Property Binding
Property Binding is bind property NOT attribute! import {Component, Input, Output, EventEmitter} fro ...
- angular学习(二)-- Directive
1.2 指令:Directive AngularJS 通过被称为 指令 的新属性来扩展 HTML, 具体表现形式一般为带有前缀 ng-xxx 的 HTML 属性. 指令的使用形式 ng-xxx 的属性 ...
- 【Angular专题】——(2)【译】Angular中的ForwardRef
原文地址:https://blog.thoughtram.io/angular/2015/09/03/forward-references-in-angular-2.html 作者:Christoph ...
- Angular之 Scope和 Directive
---------------------------Scope-------------------------------- https://docs.angularjs.org/guide/sc ...
- angular的GitHub Repository Directive Example学习
angular的GitHub Repository Directive Example学习 <!DOCTYPE html> <html ng-app="myApp" ...
随机推荐
- hdu 5147 树状数组
题意:求满足a<b<c<d,A[a]<A[b],A[c]<A[d]的所有四元组(a,b,c,d)的个数 看到逆序对顺序对之类的问题一开始想到了曾经用归并排序求逆序对,结果 ...
- jQuery ajax - serialize() 方法-输出序列化表单值
定义和用法 serialize() 方法通过序列化表单值,创建 URL 编码文本字符串. 您可以选择一个或多个表单元素(比如 input 及/或 文本框),或者 form 元素本身. 序列化的值可在生 ...
- bootstrap学习总结-03 常用标签1
1 显示段落 在HTML中,段落使用p标签包起来,重要的文字使用strong标签,em标签.<em> 标签告诉浏览器把其中的文本表示为强调的内容.对于所有浏览器来说,这意味着要把这段文字用 ...
- LightOj1089(求点包含几个线段 + 线段树)
题目链接 题意:n( n <= 50000 ) 个线段,q ( q <= 50000) 个点,问每个点在几个线段上 线段端点的和询问的点的值都很大,所以必须离散化 第一种解法:先把所有的线 ...
- SOA 和webservice 的区别
http://blog.csdn.net/bingjing12345/article/details/7575566 Web service 的具体过程 需要明确的东西 1, 服务器端 和 客户端 之 ...
- js随笔,css和js禁止网页选择文本,table的class样式使得td的class样式失效,jquery获得元素坐标
css使用user-select,user-select不是W3C标准,浏览器支持不完整:user-select有两个值,none用户不可以选择文本,text用户可以选择文本 body{-moz-us ...
- Java数据结构——容器总结
4大容器——List.Set.Queue.Map List 1.ArrayList 优点:随机访问元素 缺点:插入和移除元素时较慢 2.LinkedList 优点:插入和删除元素 缺点:随机访问方面相 ...
- 一台机子上运行使用不同Java版本的多个tomcat
方法 在tomcat/bin/下创建setenv.sh并写入 export JAVA_HOME=/usr/share/jvm/jdk1..0_91 When you starting tomcat u ...
- WebApp基础01-设置读取assets目录下文件
要读取assets下的目录,只需要修改三个地方即可 1.res/layout/activity_main.xml 2.AndroidManifest.xml 3.src\com\example\lcy ...
- MathType中如何批量修改公式字体和大小
MathType中如何批量修改公式字体和大小 关于MathType : MathType 是由美国Design Science公司开发的功能强大的数学公式编辑器,它同时支持Windows和Macint ...