Home  >  Article  >  Web Front-end  >  AngularJS's $watch method (monitoring actions)

AngularJS's $watch method (monitoring actions)

黄舟
黄舟Original
2017-02-18 13:36:161210browse

1. Problem background

The $watch method in AngularJS to monitor data changes


2. Implementation source code

<!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",[&#39;$scope&#39;,function($scope){
				$scope.count = 0;
				$scope.username = "";
				$scope.$watch(&#39;username&#39;,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. Implementation result

(1) Initialization

        AngularJSs $watch method (monitoring actions)

## (2) Change the input box value

     AngularJSs $watch method (monitoring actions)

## The above is the content of the $watch method (monitoring action) of AngularJS. For more related content, please pay attention to the PHP Chinese website (www .php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn