[AngularJS] $scope.$watch
/**
* 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的更多相关文章
- 【js类库AngularJs】web前端的mvc框架angularjs之hello world
AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.AngularJS有着诸多特性,最为核 ...
- angularJS--神奇的$scope
我们在使用angularJS时,$scope对于angularJS是非常重要的,它是angularJS的基础,但$scope到底是什么呢?下面进行一些介绍. 1.$scope是一个普通的js对象 2. ...
- JS框架~Angularjs
无意中看到anytao的项目,工作台,使用了Angularjs框架,感觉在前端表现上用户体验比较好,于是就简单看了一下,原来使用很简单,或者说,人家把代码封装的很好,以至于开发人员调用时比较简单,呵呵 ...
- AngularJS 3
AngularJS 源码分析3 本文接着上一篇讲 上一篇地址 回顾 上次说到了rootScope里的$watch方法中的解析监控表达式,即而引出了对parse的分析,今天我们接着这里继续挖代码. $w ...
- 转载:温故而知新 - AngularJS 1.x
原文: http://geek.csdn.net/news/detail/102405 温故而知新 - AngularJS 1.x
- [转][Angularjs]$http.post与$.post
本文转自:https://www.cnblogs.com/wolf-sun/p/6878868.html 摘要 在angularjs发送post请求的时候,确实很困惑,在传递json数据的时候,总会遇 ...
- Spring标签之Bean @Scope
@Bean 的用法 @Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里.添加的bean的id为方法名 定义bean 下面是@Co ...
- AngularJS 从DOM中获取scope
节选官方文档: 原文:https://docs.angularjs.org/guide/scope scope是附加在DOM上,使用了ng-app指令的DOM就是root scope.一般是<h ...
- angular中的scope
angular.element($0).scope() 什么是scope? scope是一个refer to the application model的object.它是一个expression的执 ...
- angualar入门学习-- 作用域$scope
作用域$scope: 是ng执行环境,视图与controller之间的胶水,双向绑定的基础 $scope提供里$watch方法,监听数据模型变化 $scope提供里$apply方法,将数据模型变化更新 ...
随机推荐
- NumPy 新知
import numpy as np a = np.arange(5) a array([0, 1, 2, 3, 4]) 增加一个维度: b = a[:, None] c = a[:,np.newax ...
- 计算机二级C考试有感
细节细节细节,细节决定成败,记不熟的玩意就是知识点的漏洞. 总结一下这次考试我没有掌握好的知识点. 1,sizeof() sizeof() 是计算具体所占的空间大小 char[7]就是7 int[7] ...
- JavaWeb 之 AJAX
Ajax ajax:AJAX 是与服务器交换数据的艺术,它在不重载全部页面的情况下,实现了对部分网页的更新 AJAX:Asynchronous JavaScript and XML,异步 javasc ...
- POJ3710 Christmas Game 博弈论 sg函数 树的删边游戏
http://poj.org/problem?id=3710 叶子节点的 SG 值为0:中间节点的SG值为它的所有子节点的SG值加1后的异或和. 偶环可以视作一个点,奇环视为一条边(连了两个点). 这 ...
- Codeforces 839D Winter is here(容斥原理)
[题目链接] http://codeforces.com/contest/839/problem/D [题目大意] 给出一些数,求取出一些数,当他们的GCD大于0时,将数量乘GCD累加到答案上, 求累 ...
- trie--- POJ 3764 The xor-longest Path
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5453 Accepted: 1 ...
- PHP -- 模拟测试上传文件
FROM :http://web.itivy.com/article-740-1.html QQ上传文件为什么那么快? “QQ上传大文件/QQ群发送大文件时,可以在极短的时间内完成”是如何做到的. 有 ...
- [转]ANDROID开发之SQLite详解
SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQLit ...
- Codeforces Round #346 (Div. 2) E. New Reform dfs
E. New Reform 题目连接: http://www.codeforces.com/contest/659/problem/E Description Berland has n cities ...
- mybatis源码分析(4)----org.apache.ibatis.binding包
1. 我们通过接口操作数据库时,发现相关的操作都是在org.apache.ibatis.binding下 从sqSessin 获取getMapper() SqlSession session = s ...