AngularJS之$watch方法(监控动作)

前端之家收集整理的这篇文章主要介绍了AngularJS之$watch方法(监控动作)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、问题背景

AngularJS中的$watch方法来监听数据变化


2、实现源码

<!DOCTYPE html>
<html>
	<head>
		<Meta charset="UTF-8">
		<title>AngularJS之$watch方法(监控动作)</title>
		<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
		<style>
			body{
				font-size: 12px;
				font-family: "微软雅黑";
				background-color: #F8EFC0;
				backface-visibility: visible;
			}
			div{
				margin: 10px 10px 10px 10px;
			}
		</style>
		<script>
			var app = angular.module("watchApp",[]);
			app.controller("watchCon",['$scope',function($scope){
				$scope.count = 0;
				$scope.username = "";
				$scope.$watch('username',function(){
					$scope.count++;
				});
			}]);
		</script>
	</head>
	<body ng-app="watchApp">
		<div ng-controller="watchCon">
			<div>
				<input type="text" id="username" ng-model="username" maxlength="12" autocomplete="on"/>
			</div>
			<div>
				{{count}}
			</div>
		</div>
	</body>
</html>

3、实现结果

(1)初始化


(2)改变输入框值

猜你在找的Angularjs相关文章