AngularJS 'Controller As'用法
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'用法的更多相关文章
- 学习AngularJs:Directive指令用法(完整版)
这篇文章主要学习AngularJs:Directive指令用法,内容很全面,感兴趣的小伙伴们可以参考一下 本教程使用AngularJs版本:1.5.3 AngularJs GitHub: http ...
- AngularJS中transclude用法详解
这篇文章主要介绍了AngularJS中transclude用法,详细分析了transclude的具体功能.使用技巧与相关注意事项,需要的朋友可以参考下 本文实例讲述了AngularJS中transcl ...
- Angularjs Controller 间通信机制
在Angularjs开发一些经验总结随笔中提到我们需要按照业务却分angular controller,避免过大无所不能的上帝controller,我们把controller分离开了,但是有时候我们需 ...
- 【转】Angularjs Controller 间通信机制
在Angularjs开发一些经验总结随笔中提到我们需要按照业务却分angular controller,避免过大无所不能的上帝controller,我们把controller分离开了,但是有时候我们需 ...
- 学习AngularJs:Directive指令用法
跟我学AngularJs:Directive指令用法解读(上) http://blog.csdn.net/evankaka/article/details/51232895 跟我学AngularJs: ...
- (十六)JQuery Ready和angularJS controller的运行顺序问题
项目中使用了JQuery和AngularJS框架,近期定位一个问题,原因就是JQuery Ready写在了angularJS controller之前,导致JQuery选择器无法选中须要的元素(由于a ...
- 转:AngularJS的Filter用法详解
Filter简介 Filter是用来格式化数据用的. Filter的基本原型( '|' 类似于Linux中的管道模式): {{ expression | filter }} Filter可以被链式使用 ...
- AngularJS的Filter用法详解
上一篇讲了自定义Directive,本篇是要讲到AngularJS的Filter. Filter简介 Filter是用来格式化数据用的. Filter的基本原型( '|' 类似于Linux中的管道模式 ...
- angularjs controller 继承
前沿 最近在angularjs项目当中,看到 controller 好多都是重复性的代码,在 controller 当中有好多代码很相似 function(比如 controller 下的 CRUD ...
随机推荐
- Visual Studio Online Integrations-Sync and migration
原文:http://www.visualstudio.com/zh-cn/explore/vso-integrations-directory-vs
- SQL里的EXISTS与in、not exists与not in
系统要求进行SQL优化,对效率比较低的SQL进行优化,使其运行效率更高,其中要求对SQL中的部分in/not in修改为exists/not exists 修改方法如下: in的SQL语句 SELEC ...
- linux 访问tomcat 管理页面时 You are not authorized to view this page 403(真实可用)
ava代码 收藏代码 You are not authorized to view this page. If you have not changed any configuration files ...
- javascript正则——贪婪匹配
熟悉正则的朋友都知道,正则的匹配有“贪婪”和“非贪婪”之分. “贪婪”匹配是尽可能多的匹配: 对于字符串‘aaaa’, /a+/匹配整个字符串,而非贪婪匹配/a+?/匹配的是整个字符串的第一个‘a’, ...
- document.documentElement和document.body的区别
网页中获取滚动条卷去部分的高度,可以通过 document.body.scrollTop 来获取,比如使div跟着滚动条滚动: <div id="div" style=&qu ...
- 淘宝(阿里百川)手机客户端开发日记第七篇 Service,Handler和Thread
现在我们已经已经知道android有Service,Handler和Thread这些内容了,但是我想应该还有很多人对此并不是很清楚他们之间的区别! (1)Service 是运行在后端的程序,不与UI直 ...
- [Effective JavaScript 笔记]第3章:使用函数--个人总结
前言 这一章把平时会用到,但不会深究的知识点,分开细化地讲解了.里面很多内容在高3等基础内容里,也有很多讲到.但由于本身书籍的篇幅较大,很容易忽视对应的小知识点.这章里的许多小提示都很有帮助,特别是在 ...
- 解决Yum安装依赖问题
导读 最近在网上看到很多关于安装网络Yum源是报错的求助,本小白也曾遇到过此类问题,后找过度娘没有有效的解决办法.最后,经过几番尝试后终于解决,现在将解决方案共享给大家! Yum源及安装 安装过程在之 ...
- 修改Capfile,在正式环境不再使用migration修改数据库
原因是这样的,运维不开放正式环境数据库的alter权限,所以每次都要给他们把sql语句发过去, 由他们来操作. https://github.com/capistrano/rails Require ...
- 通过IIS调试ASP.NET项目
当我们使用Visual Studio调试的时候,通常我们会选择VS自带的ASP.NET Developerment Server(也是默认选项),当第一次调试的时候(按F5或Ctrl+F5不调试直接打 ...