$scope内置的所有函数中,用得最多的可能就是$watch 函数了。当你的数据模型中某一部分发生变化时,$watch函数可以向你发出通知

你可以监控单个对象的属性,也可以监控需要经过计算的结果(函数)。

实际上只要能够被当作属性访问到,或者可以当作一个JavaScript函数被计算出来,就可以被$watch 函数监控。

它的函数签名为$watch(watchFn, watchAction, deepWatch)

  • watchFn

该参数是一个带有Angular表达式或者函数的字符串,它会返回被监控的数据模型的当前值。这个表达式将会被执行很多次,所以你要保证它不会产生 其他副作用。也就是说,要保证它可以被调用很多次而不会改变状态。基于同样的原因,监控表达式应该很容易被计算出来。如果你使用字符串传递了一个 Angular表达式,那么它将会针对调用它的那个作用域中的对象而执行。

  • watchAction

这是一个函数或者表达式,当watchFn 发生变化时会被调用。如果是函数的形式,它将会接收到watchFn的新旧两个值,以及作用域对象的引用。

其函数签名为function(newValue, oldValue, scope)。

  • deepWatch

如果设置为true,这个可选的布尔型参数将会命令Angular去检查被监控对象的每个属性是否发生了变化。如果你想要监控数组中的元素,或者对象上的所有属性,而不只是监控一个简单的值,你就可以使用这个参数。由于Angular需要遍历数组或者对象,如果集合比较大,那么运算负担就会比较重。

注销监控器

$watch 函数会返回一个函数,当你不再需要接收变更通知时,可以用这个返回的函数注销监控器。

如果我们需要监控一个属性,然后接着注销监控,我们可以使用以下代码:
...
var dereg = $scope.$watch('someModel.someProperty', callbackOnChange());

dereg();

示例代码:

<!DOCTYPE html>
<html> <head>
<meta charset="utf-8" />
<title></title>
<script src="js/angular.min.js"></script>
<link href="css/bootstrap.min.css" rel="stylesheet" />
</head> <body ng-app="myApp">
<div style="padding: 100px 500px 100px;" ng-controller="CartController">
<form class="form" role="form">
<div class="form-group" ng-repeat="item in items">
<span class="label label-default">{{item.title}}</span>
<input class="form-control" ng-model="item.quantity">
<span class="label-success"><div class="label label-info">单价:</div>{{item.price | currency}}</span>
<span class="label-success"><div class="label label-info">总价:</div>{{item.price * item.quantity | currency}}</span>
</div><br />
<div class="label label-primary">Total: {{totalCart() | currency}}</div>
<div class="label label-danger">Discount: {{discount | currency}}</div>
<div class="label label-primary">Subtotal: {{subtotal() | currency}}</div>
</form>
</div> <script src="js/CartController.js"></script>
</body> </html>

CartController.js

var app = angular.module('myApp', []);
app.controller('CartController', CartController);
CartController.$inject = ['$scope']; function CartController($scope) {
$scope.items = [{
title: 'Paint pots',
quantity: 8,
price: 3.95
}, {
title: 'Polka dots',
quantity: 17,
price: 12.95
}, {
title: 'Pebbles',
quantity: 5,
price: 6.95
}];
$scope.totalCart = function() {
var total = 0;
for (var i = 0, len = $scope.items.length; i < len; i++) {
total = total + $scope.items[i].price * $scope.items[i].quantity;
}
return total;
}
$scope.subtotal = function() {
return $scope.totalCart() - $scope.discount;
}; function calculateDiscount(newValue, oldValue, scope) {
$scope.discount = newValue > 100 ? 10 : 0;
}
var dereg = $scope.$watch($scope.totalCart, calculateDiscount);
// dereg();
}

angular $scope.$watch的更多相关文章

  1. angularJS--神奇的$scope

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

  2. Spring标签之Bean @Scope

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

  3. AngularJS 从DOM中获取scope

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

  4. Forms in Angular 2

    Input handling is an important part of application development. The ng-model directive provided in A ...

  5. angular中的scope

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

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

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

  7. AngularJS-01.AngularJS,Module,Controller,scope

    1.AngularJS 一个构建动态Web应用程序的结构化框架. 基于JavaScript的MVC框架.(  MVC ---- Model(模型).View(视图).Controller(控制器) ) ...

  8. Event Binding in Angular

    https://www.pluralsight.com/guides/angular-event-binding Introduction In this guide, we will explore ...

  9. Closure - Mimicking block scope

    The basic syntax of an anoymous function used as a block scope (often called a private scope) is as ...

  10. 理解nodejs模块的scope

    描述 原文档地址:https://docs.npmjs.com/misc/scope 所有npm模块都有name,有的模块的name还有scope.scope的命名规则和name差不多,同样不能有ur ...

随机推荐

  1. 【调试错误】找不到 System.Web.Mvc 引用

    今天调试新项目,从svn上clone下来,发现运行不了. System.Web.Mvc 显示黄色叹号 解决方案如下: 安装一个 MVC 的插件.AspNetMVC2_VS2008.exe 下载地址:h ...

  2. 【基础知识】列一下一个.Net WEB程序员需要掌握的知识

    基础部分 C# 基础语法 OOP的概念,面向对象的理解 继承 封装 多态 ASP.NET MVC (Web Form 用的越来越少,如果你不熟悉,可以不看) JavaScript 基础语法 如何在HT ...

  3. 【css系列】创建网页加载进度条

    一.最简单或者明显的方式是使用定时器 1.在网页中加入布局覆盖真实网页内容 2.使用定时器确定加载所用时间的长短,其实并不是真正的加载进度实现 <!DOCTYPE html> <ht ...

  4. HTML - 网页特殊字符大全(转)

    原文地址请跳转:https://blog.csdn.net/Iversons/article/details/78996776

  5. css3整理--text-overflow

    text-overflow语法: text-overflow : clip | ellipsis clip:表示不显示省略标记(...),而只是简单的裁切,需要在一定的高度范围内配合overflow: ...

  6. Sencha Touch 实战开发培训 视频教程 第二期 基础提高篇 预告

    “抛砖网”国内首家首创纯实战型培训机构,提供在线培训.技术指导及答疑! 团队通过360全方位技术培训+1度手把手技术指导,保证每一个学员能最快掌握实际工作技能: 让每一个学员都能站在我们的肩膀上,展翅 ...

  7. jenkins与SonarQube集成

    一.SonarQube 我的理解是,SonarQube就是一个对代码进行分析的平台,其功能可以通过插件扩展.支持多种语言,也支持静态代码检查.发现潜在bug等. 以下是参考信息: 维基百科:https ...

  8. Egret IDE中搜索,过滤文件,只搜索.ts

    刚开始忘了这个搜索条件在哪里打开了,后来找着了,记录一下 = =!

  9. centos 7.1开机/etc/rc.local脚本不执行的问题

    Centos 7.1中,/etc/rc.local是一个软链接文件.指向的是/etc/rc.d/rc.local 在Centos之前的版本我们都会将一些开机需要执行的命令加入到/etc/rc.loca ...

  10. kvm虚拟机不能使用virsh shutdownw命令关闭虚拟机的解决方法

    今天笔者在对kvm虚拟机进行管理时,使用virsh shutdown命令关闭指定的虚拟机时,发现虽然有如下的提示,但其实虚拟机却一直不会真正的关闭. 经过查看virsh命令帮助和上网查询,才得知vir ...