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. 如何在word里面插入目录

    点击“引用”->插入目录

  2. linux挂载硬盘失败,报错!

    剛把我的一顆硬碟 ( NTFS ) 接到 Ubuntu 桌機上. 然後要 mount  的時候,出現了下面的訊息: DBus error org.gtk.Private.RemoteVolumeMon ...

  3. Robot Motion(imitate)

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11065   Accepted: 5378 Des ...

  4. CoreLoation

    - (CLLocationManager *)locationManager { if (!_locationManager) { _locationManager = [[CLLocationMan ...

  5. JSONKit 简单使用

    http://blog.csdn.net/l_ch_g/article/details/8477187 例子上写的比较浅显易懂, 不过我还是稍微总结一下: 导入JSONKit.h之后 字符串转NSDi ...

  6. Mathematica 中 Minimize函数无法找到全局最小值时的解决方法

    一直使用Minimize来找到指定约束下的函数的最小值,最近发现在一个非线性函数中使用Minimize无法提供一个"全局"最小值(使用Mathematica只是用来验证算法的,所以 ...

  7. ZeroMQ安装

    一.ZeroMQ介绍 ZeroMQ是一个开源的消息队列系统,按照官方的定义,它是一个消息通信库,帮助开发者设计分布式和并行的应用程序. 首先,我们需要明白,ZeroMQ不是传统的消息队列系统(比如Ac ...

  8. Firemonkey的旁门左道[六]

    转载:http://blog.csdn.net/qustdong/article/details/9992033 今天还是讲讲和图形有关的事情,这次的难度再增加些,不是直接改源代码了, 而是通过RTT ...

  9. HDOJ 1870

    #include<stdio.h> #include<stack> #include<string.h> #include<iostream> usin ...

  10. 杭电hdoj题目分类

    HDOJ 题目分类 //分类不是绝对的 //"*" 表示好题,需要多次回味 //"?"表示结论是正确的,但还停留在模块阶 段,需要理解,证明. //简单题看到就 ...