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. Javascript的调试利器:Firebug使用详解

    转载自:http://blog.csdn.net/tianxiaode/archive/2007/09/02/1769152.aspx   一直在用firebug,可是没有这么精通,今天看到本文章觉得 ...

  2. Linux无法使用userdel删除用户和组的解决办法

    转自:http://www.linuxidc.com/Linux/2013-07/87371.htm 简述: 今天在看书的时候,看到有个实例,手痒痒的跟着做了起来...但是,出现问题了..测试的用户和 ...

  3. mysql链接数据库时报错

    今天在命令行下链接mysql数据库报错,如下: ERROR (HY000): Can't connect to MySQL server on 'ost' (113) 这是一个什么玩意呢,怎么会报这个 ...

  4. CF360B Levko and Array (二分查找+DP)

    链接:CF360B 题目: B. Levko and Array time limit per test 2 seconds memory limit per test 256 megabytes i ...

  5. 导出Excel之Epplus使用教程4(其他设置)

    导出Excel之Epplus使用教程1(基本介绍) 导出Excel之Epplus使用教程2(样式设置) 导出Excel之Epplus使用教程3(图表设置) 导出Excel之Epplus使用教程4(其他 ...

  6. Python字符串基础操作

    ==============字符串======== >>> s1='www.baidu.com' >>> type(s1) <type 'str'> & ...

  7. HNU 12847 Dwarf Tower(最短路+队列优化)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12847 解题报告:有n样物品,编号从1到n第i样物品可以通过金 ...

  8. 十条nmap常用的扫描命令

    NMap也就是Network Mapper,nmap是在网络安全渗透测试中经常会用到的强大的扫描器,功能之强大,不言而喻.下面介绍一下它的几种扫描命令.具体的还是得靠大家自己学习,因为实在太强大了. ...

  9. [BZOJ4636]蒟蒻的数列

    [BZOJ4636]蒟蒻的数列 试题描述 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个数列,初始值均为0,他进行N次操作,每次将数列[a,b)这个区间中所有比k ...

  10. Linux 制作ftp远程yum仓库

    一.下载createrepo yum install createrepo -y 二.安装vsftp软件 yum install vsftpd -y 三.将pub制作为yum仓库 把需要的rpm包拷贝 ...