[AngularJS] Best Practise - Controller
ControllerAs:
Use thecontrollerAs
syntax always as it aids in nested scoping and controller instance reference.
Bad:
<div ng-controller="MainCtrl">
{{ someObject }}
</div>
Good:
<div ng-controller="MainCtrl as main">
{{ main.someObject }}
</div>
Use the router to couple the controller declarations with the relevant views by telling each route
what controller to instantiate.
Best:
<!-- main.html -->
<div>
{{ main.someObject }}
</div>
<!-- main.html --> <script>
// ...
function config ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl',
controllerAs: 'main'
});
}
angular
.module('app')
.config(config);
//...
</script>
This avoids using $parent
to access any parent controllers from a child controller, simple hit the main
reference and you've got it. This could avoid things such as $parent.$parent
calls.
ControllerAs as this keyword:
The controllerAs
syntax uses the this
keyword inside controllers instead of $scope
. When usingcontrollerAs
, the controller is infact bound to $scope
, there is a degree of separation.
Bad:
function MainCtrl ($scope) {
$scope.someObject = {};
$scope.doSomething = function () { };
}
angular
.module('app')
.controller('MainCtrl', MainCtrl);
Because in router, already defined controllerAs:
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl',
controllerAs: 'main'
});
Threrefore, no need to use $scope.
Good:
function MainCtrl () {
this.someObject = {};
this.doSomething = function () { };
}
angular
.module('app')
.controller('MainCtrl', MainCtrl);
Also:
function MinCtrl(){
var vm = this;
vm.someObject = {};
vm.doSomething = function(){};
} angular
.module('app')
.controller('MainCtrl', MainCtrl);
These just show examples of Objects/functions inside Controllers, however we don't want to put logic in controllers...
Avoid Controller Logic:
Avoid writing logic in Controllers, delegate to Factories/Services.
Bad:
function MinCtrl(){
var vm = this;
vm.someObject = {};
vm.doSomething = function(){};
} angular
.module('app')
.controller('MainCtrl', MainCtrl);
Good:
function MainCtrl (SomeService) {
var vm = this;
vm.doSomething = SomeService.doSomething;
}
angular
.module('app')
.controller('MainCtrl', MainCtrl);
This maximises reusability, encapsulated functionality and makes testing far easier and persistent.
[AngularJS] Best Practise - Controller的更多相关文章
- Angularjs在控制器(controller.js)的js代码中使用过滤器($filter)格式化日期/时间实例
Angularjs内置的过滤器(filter)为我们的数据信息格式化提供了比较强大的功能,比如:格式化时间,日期.格式化数字精度.语言本地化.格式化货币等等.但这些过滤器一般都是在VIEW中使用的,比 ...
- angularjs 不同的controller之间值的传递
Sharing data between controllers in AngularJS I wrote this article to show how it can possible to pa ...
- Angularjs中的Controller
概念:一个应用(APP,本身也是一个大模块)是由若干个模块(module)组成的,每个模块实现一个功能.利于代码的复用. 书写格式: <!DOCTYPE html> <html ng ...
- AngularJS实战之Controller之间的通信
我们时常会在不同controller之间进行通信,接下来就介绍三种controller之间的通信方式 一.使用$on.$emit和$broadcast进行controller通信 虽然AngularJ ...
- Angularjs 中的 controller
接触过程序开发的小伙伴们对 MVC 的开发方式想必一点也不陌生,是的, angularjs 所採用的方式便是 MVVM 的开发方式,这里的 controller 即控制器 了解 controller ...
- AngularJs $anchorScroll、$controller、$document
$anchorScroll 根据HTML5的规则,当调用这个函数时,它检查当前的url的hash值并且滚动到相应的元素. 监听$location.hash()并且滚动到url指定的锚点的地方.可以通过 ...
- [AngularJS] Best Practise - Module
Module definitions Angular modules can be declared in various ways, either stored in a variable or u ...
- [AngularJS] Best Practise - Resolve promises in router, defer controllers
See more:http://toddmotto.com/opinionated-angular-js-styleguide-for-teams/ /** * Created by Answer12 ...
- angularJS+requireJS实现controller及directive的按需加载
最近因为项目的比较大,需要加载的js文件较多,为了提高首屏页面的加载速度,需要对js文件进行按需加载,然后网上参考了一些资料,自己也深入研究一番之后,实现了按需加载控制器js文件及指令js文件的效果: ...
随机推荐
- lua Date和Time
time和date两个函数在Lua中实现所有的时钟查询功能.函数time在没有参数时返回当前时钟的数值.(在许多系统中该数值是当前距离某个特定时间的秒数.)当为函数调用附加一个特殊的时间表时,该函数就 ...
- Markdown 是什么?
这是一篇 Markdown 学习笔记,简要记录常用 Markdown 语法. Markdown 是什么? Markdown 是一种轻量级标记语言,创始人为约翰·格鲁伯(John Gruber)和亚伦· ...
- scrollTop
scrollTop 表示滚动的高度,默认从position:0;开始向下滚,scrollTop(offset)的offset表示相对顶部的偏移,以像素计,<br/> scrollTop() ...
- iOS学习中的一些误区
周二拿到offer之后,周三确定了去哪家,今天周四.今天是一个例外.中午写更新. 人到了不同的阶段,最重要的就是要更新自己的方法论.也就是说,不能穿新鞋,走老路,这样人就不会有大的突破. 下面我就分析 ...
- LCA of a Binary Tree
236. Lowest Common Ancestor of a Binary Tree /** * 基础版 * 给定p,q都是在树中 * 有两种情况: * 1. p和q分布在LCA的两侧 * 2. ...
- python 字符串,数组,元祖操作基础巩固。
由于上个星期有点忙,没时间来抽空记一些有用的东西.丢了比较久的python很多忘记的小操作我也会重新捡起来 以前最容易搞混的 str.split() #操作会生成一个数组对象.example:'lap ...
- 移动Web单页应用开发实践——页面结构化
1. 前言 在开发面向现代智能手机的移动Web应用的时候,无法避免一个事实,就是需要开发单页应用(Single Page WebApp).对于不同的系统需求,单页应用的粒度会不同,可能是整个系统都使用 ...
- 适应所有浏览器的cookie
//设置cookie的方法 weiyingfunction SetCookie(a, b) { var d = new Date(); var v = arguments; ...
- AutoCAD.NET 不使用P/Invoke方式调用acad.exe或accore.dll中的接口(如acedCommand、acedPostCommand等)
使用C#进行AutoCAD二次开发,有时候由于C#接口不够完善,或者低版本AutoCAD中的接口缺少,有些工作不能直接通过C#接口来实现,所以需要通过P/Invoke的方式调用AutoCAD的其他DL ...
- RGPJS 教程之八 创造场景
开始画面 游戏画面 代码 <!DOCTYPE html> <html> <head> <script src="rpg-beta-2.js" ...