http://xlows.blog.51cto.com/5380484/1425445
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
var firstController = function ($scope){ $scope.name= '张三' ; $scope.count=0; // 监听一个model 当一个model每次改变时 都会触发第2个函数 $scope.$watch( 'name' , function (newValue,oldValue){ ++$scope.count; if ($scope.count > 30){ $scope.name = '已经大于30次了' ; } }); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<!DOCTYPE html> < html > < head > < meta charset = "utf-8" > </ head > < body > < div ng-app = "" > < div ng-controller = "firstController" > < input type = "text" value = "" ng-model = "name" /> 改变次数:{{count}}-{{name}} </ div > </ div > < script type = "text/javascript" src = "app/index.js" ></ script > < script type = "text/javascript" src = "../../vendor/angular/angularjs.js" ></ script > </ body > </ html > |
1
2
3
4
|
$scope.data = { name : '李四' , count:20 } |
1
2
3
|
$scope.$watch( 'data' , function (){ }, true ) |