您好,欢迎来到刀刀网。
搜索
您的当前位置:首页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;
	}
	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)初始化

(2)改变输入框值

Copyright © 2019- gamedaodao.com 版权所有 湘ICP备2022005869号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务