For the $watch in last article, we watch 'user.password', actually it is a string.

If you watch 'user', this object and we do something like:

  1. function WatchCtrl ($scope) {
  2.  
  3. $scope.$watch('user', function (newVal, oldVal) {
  4. console.log(newVal, oldVal);
  5. });
  6.  
  7. }

Actually it won't work.

Because, $watch without the third param is a reference watching. If we add the third param:

  1. function WatchCtrl ($scope) {
  2.  
  3. $scope.$watch('user', function (newVal, oldVal) {
  4. console.log(newVal, oldVal);
  5. }, true);
  6.  
  7. }

Then it lost value watching, but it is qiute expensive.

Actually we have another way to do that if what we are watching is an object by using $watchCollection:

  1. function WatchCtrl ($scope) {
  2.  
  3. $scope.$watchCollection('user', function (newVal, oldVal) {
  4. console.log(newVal, oldVal);
  5. });
  6.  
  7. }

[AngularJS] $scope.$warchCollection的更多相关文章

  1. 【js类库AngularJs】web前端的mvc框架angularjs之hello world

    AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.AngularJS有着诸多特性,最为核 ...

  2. angularJS--神奇的$scope

    我们在使用angularJS时,$scope对于angularJS是非常重要的,它是angularJS的基础,但$scope到底是什么呢?下面进行一些介绍. 1.$scope是一个普通的js对象 2. ...

  3. JS框架~Angularjs

    无意中看到anytao的项目,工作台,使用了Angularjs框架,感觉在前端表现上用户体验比较好,于是就简单看了一下,原来使用很简单,或者说,人家把代码封装的很好,以至于开发人员调用时比较简单,呵呵 ...

  4. AngularJS 3

    AngularJS 源码分析3 本文接着上一篇讲 上一篇地址 回顾 上次说到了rootScope里的$watch方法中的解析监控表达式,即而引出了对parse的分析,今天我们接着这里继续挖代码. $w ...

  5. 转载:温故而知新 - AngularJS 1.x

    原文: http://geek.csdn.net/news/detail/102405 温故而知新 - AngularJS 1.x

  6. [转][Angularjs]$http.post与$.post

    本文转自:https://www.cnblogs.com/wolf-sun/p/6878868.html 摘要 在angularjs发送post请求的时候,确实很困惑,在传递json数据的时候,总会遇 ...

  7. Spring标签之Bean @Scope

    @Bean 的用法 @Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里.添加的bean的id为方法名 定义bean 下面是@Co ...

  8. AngularJS 从DOM中获取scope

    节选官方文档: 原文:https://docs.angularjs.org/guide/scope scope是附加在DOM上,使用了ng-app指令的DOM就是root scope.一般是<h ...

  9. angular中的scope

    angular.element($0).scope() 什么是scope? scope是一个refer to the application model的object.它是一个expression的执 ...

  10. angualar入门学习-- 作用域$scope

    作用域$scope: 是ng执行环境,视图与controller之间的胶水,双向绑定的基础 $scope提供里$watch方法,监听数据模型变化 $scope提供里$apply方法,将数据模型变化更新 ...

随机推荐

  1. centos 6.5 安装mysql 5.6.35--libc.so.6(GLIBC_2.14)(64bit)

    [参考] http://blog.csdn.net/cpplang/article/details/8462768 http://www.linuxidc.com/Linux/2015-04/1160 ...

  2. [BZOJ4870][六省联考2017]组合数问题(组合数动规)

    4870: [Shoi2017]组合数问题 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 748  Solved: 398[Submit][Statu ...

  3. 【推导】【线段树】hdu5929 Basic Data Structure

    题意: 维护一个栈,支持以下操作: 从当前栈顶加入一个0或者1: 从当前栈顶弹掉一个数: 将栈顶指针和栈底指针交换: 询问a[top] nand a[top-1] nand ... nand a[bo ...

  4. SEL和IMP

    http://www.jianshu.com/p/4a09d5ebdc2c SEL : 类成员方法的指针,但不同于C语言中的函数指针,函数指针直接保存了方法的地址,但SEL只是方法编号. IMP:一个 ...

  5. Educational Codeforces Round 12 D. Simple Subset 最大团

    D. Simple Subset 题目连接: http://www.codeforces.com/contest/665/problem/D Description A tuple of positi ...

  6. Shell基础学习(四) echo命令

    1.显示普通的字符串 echo "boring" 2.显示转义字符 echo "\"It is a test!\""; 3.read 命令从 ...

  7. 创建Server(tomcat)时候报Cannot create a server using the selected type

    1.退出 eclipse 2.到[工程目录下]/.metadata/.plugins/org.eclipse.core.runtime/.settings 3.把org.eclipse.wst.ser ...

  8. TCP长连接与短链接

    1. TCP连接 当网络通信时采用TCP协议时,在真正的读写操作之前,server与client之间必须建立一个连接,当读写操作完成后,双方不再需要这个连接时它们可以释放这个连接,连接的建立是需要三次 ...

  9. PostgreSQL与MySQL的区别收集(转)

    特性 MySQL PostgreSQL 实例 通过执行 MySQL 命令(mysqld)启动实例.一个实例可以管理一个或多个数据库.一台服务器可以运行多个 mysqld 实例.一个实例管理器可以监视 ...

  10. uboot启动内核的实现

    前面我们分析了uboot 的整个流程,我们知道uboot启动以后所有功能都是通过命令来实现的,启动kernel就是执行了bootcmd里面的命令.命令执行过程在uboot中是非常重要的现在我们就来看u ...