Angularjs $scope 里面的$apply方法 和 $watch方法
来源:互联网 发布:网络马甲是什么意思 编辑:程序博客网 时间:2024/06/05 20:28
1. Angularjs $scope 里面的$apply 方法
$apply 方法作用:Scope 提供$apply 方法传播 Model 的变化
$apply 方法使用情景:
AngularJS 外部的控制器(DOM 事件、外部的回调函数如 jQuery UI 空间等)调用了 AngularJS 函数之后,必须调用$apply。在这种情况下,你需要命令 AngularJS 刷新自已(模型、视图等),$apply 就是用来做这件事情的。
$apply 方法注意事项:
只要可以,请把要执行的代码和函数传递给$apply 去执行,而不要自已执行那些函数然后再调用$apply。
例如,你应该像下面这样来执行你的代码:
<script type="text/javascript">var app = angular.module("myApp", []);app.controller('firstController',function($scope){$scope.name = 'hello';setTimeout(function(){//$scope.name = 'hi';$scope.$apply(function(){$scope.name = 'hi';});},2000);/*$timeout(function(){$scope.name = 'hi';},2000);*/$scope.show = function(){$scope.name = 'hi 点击事件发生了';};});</script>
2. Angularjs $scope 里面的$watch 方法
$watch 方法作用:
$watch 方法监视 Model 的变化。
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script type="text/javascript" src="../angular.min.js"></script></head><body><div ng-app="myApp"><div ng-controller="CartController"><p>价格:<input type="text" ng-model="iphone.money"></p><p>个数:<input type="text" ng-model="iphone.num"></p><p>费用:<span>{{ sum() | currency:'¥' }}</span></p><p>运费:<span>{{iphone.fre | currency:'¥'}}</span></p><p>总额:<span>{{ sum() + iphone.fre | currency:'¥'}}</span></p></div></div><script type="text/javascript">var app = angular.module("myApp", []);app.controller('CartController',function($scope){$scope.iphone = {money : 5,num : 1,fre : 10};$scope.sum = function(){return $scope.iphone.money * $scope.iphone.num;};/*$scope.$watch('iphone.money',function(newVal,oldVal){console.log(newVal);console.log(oldVal);},true);*/$scope.$watch($scope.sum,function(newVal,oldVal){//console.log(newVal);//console.log(oldVal);$scope.iphone.fre = newVal >= 100 ? 0 : 10;});});</script></body></html>
0 0
- Angularjs $scope 里面的$apply方法 和 $watch方法
- Angularjs $scope 里面的$apply 方法 和 $watch 方法
- AngularJS基础——$scope里的$apply方法和$watch方法
- angularJS--apply() 、digest()和watch()方法
- angularJS<六、$scope里的$watch方法>
- angularJS<$scope里的$watch方法>
- angularjs $scope.$apply 方法详解
- angularJS学习小结——$apply方法和$watch方法
- (12)AngularJS 1.X 之$watch和$apply方法
- angularJS $scope的$watch方法监听model改变
- AngularJs学习笔记(4)--$scope里的$watch方法
- AngularJs学习笔记(3)--$scope中的$apply和$digest方法
- angularJS $scope的$apply方法实现model刷新
- [AngularJS面面观] 4. scope中的$apply方法 - Digest Cycle的触发者
- $scope里的$apply、$digest方法
- angularjs $scope.$watch 遇到的问题
- angularJS之$apply()方法
- angularJS之$apply()方法
- 基于大规模语料的新词发现算法
- 如何准确的计算并提高APP用户留存?
- SQL PRIMARY KEY 约束 高级编程
- 浅析Linux 中Java环境变量设置
- 更改cmd的默认路径
- Angularjs $scope 里面的$apply方法 和 $watch方法
- 心得
- mongodb设置数据库用户,权限,java mongodb driver3.0驱动用户验证
- linux公社 u-boot下载
- Dijkstra算法解题报告——"大一水手"
- HTML学习笔记之简单网页属性设置
- noip2015初赛入围名单
- spring 配置文件
- C++继承 派生类中的内存布局(单继承、多继承、虚拟继承)