angularjs中使用$scope.$watch监控对象模型的变化
2016-09-17 11:56
671 查看
如果对象模型发生变化时,可以使用$scope.$watch监控变化
稍微改一下,输出修改前后的内容:
<html ng-app="myApp"> <head> <title>angularjs-demo</title> <script type="text/javascript" src="lib/angularjs/angular.min.js" charset="utf-8"></script> </head> <body ng-controller="ctrl"> <input type="text" ng-model="text1" ng-model-options="{ updateOn: 'blur' }" /> <div>输入框内容改变次数:{{count}}</div> </body> <script type="text/javascript"> var app = angular.module('myApp', []) .controller('ctrl', function($scope){ $scope.count = 0; $scope.$watch('text1',function(){ $scope.count++; }); }); </script> </html>
稍微改一下,输出修改前后的内容:
<html ng-app="myApp"> <head> <title>angularjs-demo</title> <script type="text/javascript" src="lib/angularjs/angular.min.js" charset="utf-8"></script> </head> <body ng-controller="ctrl"> <input type="text" ng-model="text1" ng-model-options="{ updateOn: 'blur' }" /> <div>输入框内容改变次数:{{count}}</div> <div>原内容:{{content1}}</div> <div>新内容:{{content2}}</div> </body> <script type="text/javascript"> var app = angular.module('myApp', []) .controller('ctrl', function($scope){ $scope.count = 0; $scope.$watch('text1',function(newValue,oldValue){ $scope.count++; $scope.content1 = oldValue; $scope.content2 = newValue; }); }); </script> </html>
相关文章推荐
- angularJS使用$watch监控数据模型的变化
- 使用信号监控 Django 模型对象字段值的变化
- angularJS使用$watch监控数据模型的变化
- --@angularJS--$scope.watch监听模型变化
- angularjs于directive声明scope说明何时以及如何使用对象修饰符
- angular 监控数据模型变化的事件方法$watch
- scope 对象在 AngularJS 中充当数据模型的作用,也就是一般 MVC 框架中 Model 得角色.但又不完全与通常意义上的数据模型一样,因为 $scope 并不处理和操作数据,它只是建立了
- Angularjs 利用 $on、$emit和$broadcast传值,利用$watch监听模型变化
- 利用信号如何监控Django模型对象字段值的变化详解
- 使用watch监听路由变化和watch监听对象的实例
- AngularJS v1.6.3,监听数据变化$scope.$watch不生效问题
- 使用watch监听路由变化和watch监听对象
- AngularJS 监控对象属性:$watch和$digest
- angularjs UI控制其分离,scope应用及$watch观察模型变化
- ZZ:使用.NET语言开发Silverlight应用程序入门(二):界面和对象模型
- ZZ:使用.NET语言开发Silverlight应用程序入门(二):界面和对象模型
- 使用对象的 ADO 编程模型
- ZZ:使用.NET语言开发Silverlight应用程序入门(二):界面和对象模型
- ZZ:使用.NET语言开发Silverlight应用程序入门(二):界面和对象模型
- Web程序,使用ReportSerivce报表对象模型编程发布报表