The post we have: http://www.cnblogs.com/Answer1215/p/4185504.html gives a breif introduce about bindToController on the directive.

Here is a blog about bindToController from thoughtramwhich explains in detail why bingToController comes into handy and why controllerAs syntax is important.

Here is the part which useful to myself

This is a directive with an isolated scope that defines how its scope properties are bound. Alright, let’s say we have directive with an isolated scope, a controller and a template that uses the controller properties accordingly:

app.directive('someDirective', function () {
return {
scope: {},
controller: function () {
this.name = 'Pascal'
},
controllerAs: 'ctrl',
template: '<div>{{ctrl.name}}</div>'
};
});

Easy. That works and we knew that already. Now to the tricky part: what ifname should be two-way bound?

app.directive('someDirective', function () {
return {
scope: {
name: '='
},
// ...
};
});

Changes to isolated scope properties from the outside world are not reflected back to the controllers’ this object. What we need to do to make this work in 1.2, is to use the $scope service to re-assign our scope values explicitly, whenever a change happens on a particular property. And of course, we mustn’t forget to bind our watch callback to the controllers’ this:

app.directive('someDirective', function () {
return {
scope: {
name: '='
},
controller: function ($scope) {
this.name = 'Pascal'; $scope.$watch('name', function (newValue) {
this.name = newValue;
}.bind(this));
},
// ...
};
});

Here we go… the $scope service we initially got rid off is now back. If you now think this is crazy, especially when considering that this is just one scope property and in a real world directive you usually have more than one, then my friend, I agree with you.

Luckily, this is no longer a problem in Angular 1.3!

app.directive('someDirective', function () {
return {
scope: {
name: '='
},
controller: function () {
this.name = 'Pascal';
},
controllerAs: 'ctrl',
bindToController: true,
template: '<div>{{ctrl.name}}</div>'
};
});

[AngularJS - thoughtram] Exploring Angular 1.3: Binding to Directive Controllers的更多相关文章

  1. [译]Exploring Angular 1.3: Binding to Directive Controllers

    原文: http://blog.thoughtram.io/angularjs/2015/01/02/exploring-angular-1.3-bindToController.html Angul ...

  2. AngularJS进阶(四)ANGULAR.JS实现下拉菜单单选

    ANGULAR.JS: NG-SELECT AND NG-OPTIONS PS:其实看英文文档比看中文文档更容易理解,前提是你的英语基础还可以.英文文档对于知识点讲述简明扼要,通俗易懂,而有些中文文档 ...

  3. [Angular] Use Angular components in AngularJS applications with Angular Elements

    When migrating AngularJS (v1.x) applications to Angular you have different options. Using Angular El ...

  4. 【js类库AngularJs】解决angular+springmvc的post提交问题

    [js类库AngularJs]解决angular+springmvc的post提交问题 AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前 ...

  5. 来自 Thoughtram 的 Angular 2 系列资料

    Angular 2 已经正式 Release 了,Thoughtram 已经发布了一系列的文档,对 Angular 2 的各个方面进行深入的阐释和说明. 我计划逐渐将这个系列翻译出来,以便对大家学习 ...

  6. [AngularJS] Using the Angular scope $destroy event and method

    With Angular scopes, you have access to a $destroy event that can be used to watch $scope events. Th ...

  7. [AngularJS] Lazy loading Angular modules with ocLazyLoad

    With the ocLazyLoad you can load AngularJS modules on demand. This is very handy for runtime loading ...

  8. [AngularJS] New in Angular 1.3 - Performance Boost with debugInfoEnabled

    By default, Angular provides a lot of debug information on the DOM that's only necessary for tools l ...

  9. [AngularJS] New in Angular 1.5 ng-animate-swap

    <!DOCTYPE html> <html ng-app="MyApplication"> <head> <link rel=" ...

随机推荐

  1. poj2391,poj2455

    这两题本质是一致的: 一般来说,对于最长(短)化最短(长)的问题我们一般都使用二分答案+判定是否可行 因为这样的问题,我们一旦知道答案,就能知道全局信息 拿poj2455举例,对于二分出的一个答案,我 ...

  2. HDU 1532 Drainage Ditches 排水渠(最大流,入门)

    题意: 给出一个有向图,以及边上的容量上限,求最大流.(有重边,要将容量上限叠加) 思路: 用最简单的EK+BFS解决.每次搜到一条到达终点的路径,就立刻退出,更新ans,然后再回头修改图中的当前fl ...

  3. jquery Ajax应用总结

    常见应用: 下面是Jquery中AJAX参数详细列表: 参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. type String (默认: "GET&quo ...

  4. ShopNc商城修改详情

    1. 修改400电话.(400.png) a.位置:在页面顶部搜索的后面. b.修改文件: t/layout/home_layout.php 增标签 <img border=0 src=&quo ...

  5. colspan在浏览器中失效的问题

    <table border=" style="border-collapse:collapse;"> <tr> <td>def< ...

  6. EF Code First学习笔记:数据库创建

    控制数据库的位置 默认情况下,数据库是创建在localhost\SQLEXPRESS服务器上,并且默认的数据库名为命名空间+context类名,例如我们前面的BreakAway.BreakAwayCo ...

  7. sqlserver 2008 R2 分区表测试

    有一张表期中有100多w条数据 程序执行起来比较慢,想用分区表的办法,使查询变快一些. 方案如下 --查看分区信息SELECT * FROM sys.partition_range_values -- ...

  8. Hadoop2.7.2安装笔记

    1.设置免密SSH登录 $ ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa $ cat ~/.ssh/id_dsa.pub >> ~/.ssh/autho ...

  9. Windows mysql 5.6 zip 安装 并创建用户赋予数据库权限

    1.下载mysql 5.6 zip 首先下载mysql 5.6 zip (在官网(http://www.mysql.com/downloads/) 或者其他网站都行), 然后解压在自己的电脑上 D:\ ...

  10. activemq 的小实验

    package ch02.chat; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms ...