/**
* Created by Answer1215 on 11/13/2014.
*/ function MainCtrl($scope){ function isLongEnough (pwd) {
return pwd.length > 4;
} function hasNumbers (pwd) {
return /[0-9]/.test(pwd);
} this.watchPwd = function(newVal, oldVal){
//first init, you might got undefined
if (!newVal) return;
$scope.reqs = []; if (!isLongEnough(newVal)) {
$scope.reqs.push('Too short');
} if (!hasNumbers(newVal)) {
$scope.reqs.push('Must include numbers');
} $scope.showReqs = $scope.reqs.length;
} //user.password is a string
//user is an object
$scope.$watch('user.password',this.watchPwd); //$watch can accept the third argument, once add it, angularJS
//will do lose value checking instead of reference checking, it's quite
//expensive
//in all $watch is expensive, use ng-change if you can
// $scope.$watch('user.password',this.watchPwd, true);
} angular.module('app',[])
.controller('MainCtrl', MainCtrl);

$watch can accept the third arguement, once set it as true:

$scope.$watch('user.password',this.watchPwd, true);

AngularJS will do lose value checking instead of reference checking, it is quite expensive.

For best pratise: avoid using $watch as much as you can, instead using ng-change.

<input ng-model="myModel" ng-change="callback">
<!--
$scope.callback = function () {
// go
};
-->
<!DOCTYPE html>
<html ng-app="app">
<head lang="en">
<meta charset="UTF-8">
<title>$watch</title>
<script src="bower_components/angular/angular.min.js"></script>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"/>
<style type="text/css">
.panel {
width: 70%;
margin: 30px auto;
}
</style>
</head>
<body ng-controller="MainCtrl"> <div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Create Account</h3>
</div>
<div class="panel-body">
<form role="form">
<div class="form-group">
<label for="eml">Email address</label>
<input id="eml"
type="email"
class="form-control"
placeholder="Email address"
ng-model="user.email"/>
</div> <div class="form-group">
<label for="pwd">Password</label>
<input id="pwd"
type="password"
class="form-control"
placeholder="Password"
ng-model="user.password" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
<div class="panel panel-danger" ng-show="showReqs">
<div class="panel-heading">
<h3 class="panel-title">Password Requirement</h3>
</div>
<div class="panel-body">
<ul>
<li ng-repeat="req in reqs">{{req}}</li>
</ul>
</div>
</div>
<script src="app.js"></script>
</body>
</html>

Read More: https://egghead.io/lessons/angularjs-the-basics-of-scope-watch

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

  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. java 数组的基本概念

    1.数组的基本概念 数组指的是一组相关变量的集合.例如:如果说现在要定义100个整型变量,如果按照我们以前的思路,我们可能这样定义: public class demo { public static ...

  2. 无框架完整搭建安卓app及其服务端(一)

    技术背景: 我的一个项目做的的是图片处理,用 python 实现图片处理的核心功能后,想部署到安卓app中,但是对于一个对安卓和服务器都一知半解的小白来说要现学的东西太多了. 而实际上,我们的项目要求 ...

  3. 小程序登陆遇到 ERR_REQUEST_PARAM

    小程序测试环境突然登陆不上,返回的错误信息是{}"code":-1,"error":"ERR_REQUEST_PARAM"}. 小程序登陆代 ...

  4. Unity 2D游戏开发教程之为游戏场景添加多个地面

    Unity 2D游戏开发教程之为游戏场景添加多个地面 为游戏场景添加多个地面 显然,只有一个地面的游戏场景太小了,根本不够精灵四处活动的.那么,本节就来介绍一种简单的方法,可以为游戏场景添加多个地面. ...

  5. WSDL语法

    <什么是WSDL语言> WSDL(网络服务描述语言,Web Services Description Language)是一门基于 XML 的语言,用于描述 Web Services 以及 ...

  6. 【BZOJ 2555】 2555: SubString (SAM+LCT)

    2555: SubString Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 2548  Solved: 762 Description 懒得写背景了 ...

  7. 解决关于stack溢出的问题

    开发中经常遇到: 前端遇到Uncaught RangeError: Maximum call stack size exceeded错误 后台遇到java.lang.OutOfMemoryError: ...

  8. Codeforces Round #479 (Div. 3)

    手速场2333,这群人贼牛逼!手速贼快!   A. Wrong Subtraction time limit per test 1 second memory limit per test 256 m ...

  9. bzoj 1911: [Apio2010]特别行动队 -- 斜率优化

    1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MB Description Input Output Sample Input 4 ...

  10. 2013新mac air直接安装windowns系统无法下一步 键盘鼠标失灵 无法新建分区提示gpt分区解决方案

    今天同学拿来了2013款MacbookAir,叫我帮忙装一个Win7系统,保留原来的Mac系统,于是开工.按照教程,准备一个8g的U盘,先进入Air自带的Mac系统(俗称”小牛”),将Win7旗舰版6 ...