传统controller与controllerAs(前面为传统,后面为controllerAs,分割线分隔):

路由模块:

.state('home.packing', {
url: '/packing',
templateUrl: '/app/home/packing.html',
controller: 'home.packingController'
})

----------------------------------------------------------------

.state('home.packing', {
url: '/packing',
templateUrl: '/app/home/packing.html',
controller: 'home.packingController as vm',
})//或者下面写法
.state('home.packing', {
url: '/packing',
templateUrl: '/app/home/packing.html',
controller: 'home.packingController',
controllerAs: 'vm'
})

控制器模块:

(function () {
angular.module('app')
.controller('home.packingController',['$scope','Core',packingController])
function packingController($scope,Core) {
var context = $scope;
     context.msg = 'controller' 
context.onClickChangeService = onClickChangeService;
function onClickChangeService (msg) {
console.log(msg)
}
}
})();

----------------------------------------------------------------

(function () {
angular.module('app')
.controller('home.packingController',['Core',packingController])
function packingController(Core) {
var self = this;//self指View Module
self.msg = 'controllerAs'
self.onClickChangeService = onClickChangeService;
function onClickChangeService (msg) {
console.log(msg)
}
}
})();

html模块:

<div>{{msg}}</div>
<div ng-click="onClickChangeService(msg)">点击</div>

----------------------------------------------------------------

<div>{{vm.msg}}</div>
<div ng-click="vm.onClickChangeService(vm.msg)">点击</div>

为什么使用controllerAs:

1.$scope是基于原型进行继承的,比如说当我们查找一个user对象时,angular会先查找当前$scope有没有user,如果没有的话就继续往上层$scope查找,直至$rootScope。

而在controllerAs中,假设我们使用controllerAs

UserCtrl as ctrl 
angular将控制器自身挂载在$scope上,user也变为ctrl.user,就不会存在上述的一层层查找的过程。在很多情况下,比如在嵌套的路由中,上述$scope基于原型的查找,有时候确实会提供一些便利,但这些都可以用服务来实现,也应该使用服务来实现。

2.大家在初次接触angular时一定会被推荐过将所有数据都绑定在$scope的一个对象上(比如$scope.data)来避免一些js中值的复制和对象的引用可能会造成的一些问题(公司里的新人大部分也确实遇到过这类问题),而使用controllerAs后就不需要这一步了,因为人家本来就是。

3.因为不使用$scope也就不能使用$on,$watch,$emit之类的方法,这些方法本来就应该尽量少用,这样就可以更好的控制项目中的代码,当不得不用这类方法时可以参考下面的案例。

4.便于新人学习,我发现新人对于$scope这个东西往往无法理解,而用controllerAs vm之后,则将vm(view model的简写)作为视图模型则比较好理解。

5.在一个页面中如果使用嵌套控制器,更加清晰,并且可以避免让 $scope 默认继承父组件的内容(因为在子控制器中没有声明也会继承父控制器中的内容,造成显示错误数据)

参看链接:使用controller代替$scope

传统controller与controllerAs的更多相关文章

  1. angularJS1笔记-(13)-自定义指令(controller和controllerAs实现通信)

    index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  2. [AngularJS + Unit Testing] Testing Directive's controller with bindToController, controllerAs and isolate scope

    <div> <h2>{{vm.userInfo.number}} - {{vm.userInfo.name}}</h2> </div> 'use str ...

  3. AngularJS之指令中controller与link(十二)

    前言 在指令中存在controller和link属性,对这二者心生有点疑问,于是找了资料学习下. 话题 首先我们来看看代码再来分析分析. 第一次尝试 页面: <custom-directive& ...

  4. angular controller as syntax vs scope

    今天要和大家分享的是angular从1.2版本开始带来了新语法Controller as.再次之前我们对于angular在view上的绑定都必须使用直接的scope对象,对于controller来说我 ...

  5. [AngularJS] Best Practise - Controller

    ControllerAs: Use thecontrollerAs syntax always as it aids in nested scoping and controller instance ...

  6. [AngularJS] Accessing The View-Model Inside The link() When Using controllerAs

    If u using controller & controllerAs in directive, then the link()'s 4th param 'controller' will ...

  7. angularJS 系列(五)--controller AS 语法

    原文: http://www.cnblogs.com/whitewolf/p/3493362.html 这篇国外的文章也非常好: http://codetunnel.io/angularjs-cont ...

  8. angular directive 的controllerAs的用法

    原文: https://stackoverflow.com/questions/31857735/using-controlleras-with-a-directive --------------- ...

  9. Part 32 AngularJS controller as syntax

    So far in this video series we have been using $scope to expose the members from the controller to t ...

随机推荐

  1. 用C#实现DES加密解密解决URL参数明文的问题

    啥也不说,直接上代码. 加密解码,封装到一个类,key可以自己修改. using System; using System.Security.Cryptography; using System.Te ...

  2. JS--我发现,原来你是这样的JS:面向对象编程OOP[2]--(创建你的那个对象吧)

    一.介绍 我们继续面向对象吧,这次是面向对象编程的第二篇,主要是讲创建对象的模式,希望大家能从博客中学到东西. 时间过得很快,还是不断的学习吧,为了自己的目标. 二.创建对象 1.前面的创建对象方式 ...

  3. B-树&B+树以及其在数据库中的应用

    B-树&B+树以及其在数据库中的应用 1 .B-树定义 B-树是一种平衡的多路查找树,它在文件系统中很有用. 定义:一棵m 阶的B-树,或者为空树,或为满足下列特性的m 叉树:⑴树中每个结点至 ...

  4. .net SignalR winform 推送广播

    最近在做一个项目,需要用到服务端主动推送给客户端,最开始用的是自己比较顺手的Remoting,可后来发现把服务端架到外网上,就猴子它哥了,后来又尝试WCF,虽然能推送,但是推了几次也猴子它哥了,后来找 ...

  5. 分享一篇vue项目规范

    最近 Vue 用的比较多,而且因为公司里有实习生,当几个人写一个项目的时候,会出现很多问题,最麻烦的就是规范不统一,之前我有一篇文章是说, vue 是比较有规范的一种框架了,但是也会出现很多问题,所以 ...

  6. Less命名空间

    Less命名空间 当我们拥有了大量选择器的时候,特别是团队协同开发时,如何保证选择器之间重名问题?如果你是 java 程序员或 C++ 程序员,我猜你肯定会想到命名空间 Namespaces. Les ...

  7. 0:A+B Problem-poj

    0:A+B Problem 总时间限制:  1000ms 内存限制:  65536kB 描述 Calculate a + b 输入 Two integer a,,b (0 ≤ a,b ≤ 10) 输出 ...

  8. jsp加java连接数据库,进行信息输入,并进行初步的拦截判断。

    图形大概这样 按照图片要求设计添加新课程界面.(0.5分) 在后台数据库中建立相应的表结构存储课程信息.(0.5分) 实现新课程添加的功能. 要求判断任课教师为王建民.刘立嘉.刘丹.王辉.杨子光五位教 ...

  9. Spring集成Redis缓存

    作者:13 GItHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 整合Redis 本来以为类似的Redis教程和整合代码应该会很多,因 ...

  10. 10970 - Big Chocolate

    题意 :已知n*m的巧克力,问需要掰多少次能让巧克力成为最小的一块: #include<iostream> using namespace std; int main() { int n, ...