当前位置:Gxlcms > JavaScript > AngularJS之$watch方法(监控动作)

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

时间:2021-07-01 10:21:17 帮助过:6人阅读

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;
			}
			p{
				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">
		<p ng-controller="watchCon">
			<p>
				<input type="text" id="username" ng-model="username" maxlength="12" autocomplete="on"/>
			</p>
			<p>
				{{count}}
			</p>
		</p>
	</body>
</html>


3、实现结果

(1)初始化

1175.png

(2)改变输入框值

1176.png

以上就是AngularJS之$watch方法(监控动作)的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!

人气教程排行