Angular 事件监听$scope.$watch

监听服务$watch
	1、不需要在控制器脚本中注入,直接使用
	2、监听任何想要监听的NG变量的值,并在这个值改变的时候调用回调函数

	语法:$scope.$watch('想要监听的NG变量(不需要加$scope.)或字符串',回调函数)

代码示例:

<html ng-app='app' ng-controller='main' >
<head>
	<meta charset="utf-8">
	<meta name='viewport' content='width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0'>
	<title ng-bind='mainTitle'></title>

	<script src='js/angular.js'></script>
	<script src='js/angular.route.min.js'></script>	
	<style>

	</style>
</head>
<body >

<input type="text" ng-model='in'>

<script>
	var app=new angular.module('app',[]);


	app.controller('main',['$scope',function($scope){
			$scope.in='';
			$scope.$watch('in',function(){console.log('他变了')});

	}])
</script>
</body>
</html>
发布了300 篇原创文章 · 获赞 3 · 访问量 6393

猜你喜欢

转载自blog.csdn.net/weixin_43294560/article/details/104050300