AngularJS 1.2版本中提供了Controller As语法,简单说就是可以在Controller中使用this来替代$scope,使得Controller更像一个传统的JS类,相对于$scope的继承树要理解上要简单一些。

基础用法

传统的Controller是这样写的:

app.controller('MainCtrl', function($scope) {
$scope.title = 'Some title';
});

这种写法下,$scope是注入到MainCtrl中的服务,是Dom元素和Controller之间的粘合剂。

<div ng-controller="MainCtrl">
{ { title } }
</div>

这个孤单的title常常让初学者疑惑。

在AngularJS 1.2版本之后,我们可以这样写:

app.controller('MainCtrl', function() {
this.title = 'Some title';
});

这种写法下,MainCtrl更像是一个JS类:

var MainCtrl = function() {
this.title = 'Some title';
};
var main = new MainCtrl();

在页面上使用时就可以使用Controller As语法,实例化对象

<div ng-controller="MainCtrl as main">
{ { main.title } }
</div>

嵌套块

这种理解上的好处在嵌套块中更加明显:

<div ng-controller="MainCtrl">
{ { title } }
<div ng-controller="AnotherCtrl">
{ { title } }
<div ng-controller="YetAnotherCtrl">
{ { title } }
</div>
</div>
</div>

这个title可能是$scope继承树上的任意一个。而使用Controller as之后:

<div ng-controller="MainCtrl as main">
{ { main.title } }
<div ng-controller="AnotherCtrl as another">
Scope title: { { another.title } }
Parent title: { { main.title } }
<div ng-controller="YetAnotherCtrl as yet">
Scope title: { { yet.title } }
Parent title: { { another.title } }
Parent parent title: { { main.title } }
</div>
</div>
</div>

这就清晰很多。

Directive用法

在Directive中,我们可以这样使用:

app.directive('myDirective', function() {
return {
restrict: 'EA',
template: '<div>{ { my.title } }</div>',
controller: function() {
this.title = 'Some title';
},
controllerAs: 'my'
};
});

$watch

只是$watch还是需要注入$scope:

app.controller('MainCtrl', function($scope) {
this.title = 'Some title'; $scope.$watch(angular.bind(this, function() {
return this.title;
}), function(newVal, oldVal) {});
});

本文地址:http://corncandy.github.io/2014/06/06/angularjs-controller-as/

AngularJS 'Controller As'用法的更多相关文章

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

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

  2. AngularJS中transclude用法详解

    这篇文章主要介绍了AngularJS中transclude用法,详细分析了transclude的具体功能.使用技巧与相关注意事项,需要的朋友可以参考下 本文实例讲述了AngularJS中transcl ...

  3. Angularjs Controller 间通信机制

    在Angularjs开发一些经验总结随笔中提到我们需要按照业务却分angular controller,避免过大无所不能的上帝controller,我们把controller分离开了,但是有时候我们需 ...

  4. 【转】Angularjs Controller 间通信机制

    在Angularjs开发一些经验总结随笔中提到我们需要按照业务却分angular controller,避免过大无所不能的上帝controller,我们把controller分离开了,但是有时候我们需 ...

  5. 学习AngularJs:Directive指令用法

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

  6. (十六)JQuery Ready和angularJS controller的运行顺序问题

    项目中使用了JQuery和AngularJS框架,近期定位一个问题,原因就是JQuery Ready写在了angularJS controller之前,导致JQuery选择器无法选中须要的元素(由于a ...

  7. 转:AngularJS的Filter用法详解

    Filter简介 Filter是用来格式化数据用的. Filter的基本原型( '|' 类似于Linux中的管道模式): {{ expression | filter }} Filter可以被链式使用 ...

  8. AngularJS的Filter用法详解

    上一篇讲了自定义Directive,本篇是要讲到AngularJS的Filter. Filter简介 Filter是用来格式化数据用的. Filter的基本原型( '|' 类似于Linux中的管道模式 ...

  9. angularjs controller 继承

    前沿 最近在angularjs项目当中,看到 controller 好多都是重复性的代码,在 controller 当中有好多代码很相似 function(比如 controller 下的 CRUD ...

随机推荐

  1. GPRS GPRS(General Packet Radio Service)是通用分组无线服务技术的简称,它是GSM移动电话用户可用的一种移动数据业务,属于第二代移动通信中的数据传输技术

    GPRS 锁定 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . GPRS(General Packet Radio Service)是通用分组无线服务技术的简称,它是GSM移动电话用户可 ...

  2. 10GE---超长距离的万兆以太网

    万兆以太网 锁定 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . 以太网标准是一个古老而又充满活力的标准.自从1982年以太网协议被IEEE采纳成为标准以后,已经历了20年的风风雨雨.在 ...

  3. GNU make 升级

    网上下载新版本的make文件后 在make目录下 ./configure make make install mv make /opt/mv_pro_5.0.0/montavista/common/b ...

  4. 搭个 Web 服务器(一)

    导读 我相信,如果你想成为一个更好的开发者,你必须对日常使用的软件系统的内部结构有更深的理解,包括编程语言.编译器与解释器.数据库及操作系统.Web 服务器及 Web 框架.而且,为了更好更深入地理解 ...

  5. Centos6.5更新e1000网卡驱动

    导读 在工作过程中经常遇到linux的操作系统网络不正常的情况,以前没有注意到,今天查看系统日志发现原来是网络驱动的问题.索性直接更新系统,更新网卡 问题:linux系统经常出现断网的情况,重启之后系 ...

  6. sqlMapConfig.xml配置文件详解

    sqlMapConfig.xml配置文件详解: Xml代码 Xml代码  <? xml version="1.0" encoding="UTF-8" ?& ...

  7. ubuntu硬盘安装卡在探测文件系统

    在硬盘安装ubuntu的时候,会出现这样的问题:安装程序一直卡在正在探测文件系统就不动了.解决的方法很简单.在安装之前要在终端输入sudo空格umount空格 -l空格 /isodevice 不能少一 ...

  8. PHP编译支持mysqli

    PHP编译支持mysqli前提是必须安装mysql直接上命令先进入源码包我的源码包是在/usr/local/php-5.2.1/ext/mysqli这样进入 cd /usr/local/php-5.2 ...

  9. Fast Power

    Calculate the a^n % b where a, b and n are all 32bit integers. Example For 2^31 % 3 = 2 For 100^1000 ...

  10. 【转】maven命令背后是如何工作的

    转载自:http://yinny.iteye.com/blog/1883488 Maven强大的一个重要的原因是它有一个十分完善的生命周期模型(lifecycle),它有三套相互独立的生命周期,请注意 ...