[译]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" ...
随机推荐
- Jenkins 2.x版本的节点配置选项更新
[Dumb slave]改成了[Permanent Agent],效果一致 参考:http://serverfault.com/questions/793619/jenkins-trying-to-a ...
- Android成长日记-使用ViewFlipper实现屏幕切换动画效果
(一) ViewFlipper介绍 Android系统自带的一个多页面管理控件,它可以实现子界面的自动切换 (二) 为ViewFlipper加入View 1. 静态导入:在Layout布局文件中直接导 ...
- ANDROID版本号和版本名称的重要性介绍
当我们在刚开始学习ANDROID的时候,可能不会过多的关注这个位于manifest.xml文件中的versionCode和versionName. 但是其实一个好的版本控制,对于我们有至关重要的作用. ...
- 解决Cookie乱码问题
写了一个cookie的定义和获取,结果我输入中文后,页面报错 报错信息如下: type Exception report message An exception occurred processin ...
- JZOJ P1817:[8.27]研究性学习作业
传送门 老师良心推荐的二分题.7月29号第一次写,想到了裸的DP,乱搞搞过了6组,欲优化,无解,弃疗. 然后今天老师给了题解,简单看了一下. 正解是二分答案+DP验证. 二分天数$day$,然后对于每 ...
- SQL Server 2012 学习笔记1
1. 新建的数据库会产生两个文件(数据文件.mdf 和日志文件.ldf) 2. 编辑表格和为表格录入数据 "Design"为设计表格,"Edit Top 200 Rows ...
- 20145212 《Java程序设计》第8周学习总结
20145212 <Java程序设计>第8周学习总结 教材学习内容总结 第十四章 NIO与NIO2 认识NIO NIO使用频道(Channel)来衔接数据节点,在处理数据时,NIO可以让你 ...
- JavaScript排序算法——堆排序
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 学海无涯的整理Ing..........
1.文章:Understanding JavaScript Function Invocation and “this” http://yehudakatz.com/2011/08/11/unders ...
- JVM 小结
1.jvm虚拟机的种类及特点 Sun Classic:解释和编译不能协调使用:使用基于句柄的对象查找. Exact VM:解释和编译能协调使用:采用准确式内存管理:可以知道内存中某个位置的数据是什么类 ...