Detailed explanation of $watch method in angular

黄舟
Release: 2017-09-25 10:03:37
Original
1489 people have browsed it

Dirty check is mentioned in the $apply method. First, the apply method will trigger the evel method. When the evel method is parsed successfully, the digest method will be triggered, and the digest method will trigger the watch method.

(1)$watch introduction

When digest is executed, if the value observed by watch is different from the last execution, it will be triggered.

The watch inside AngularJS realizes the timely updating of the page with the model.

The $watch method is mainly used to manually monitor an object, but an event is triggered when the object changes.

(2)watch method usage

$watch(watchFn,watchAction,deepWatch)
Copy after login

watchFn: string of angular expression or function

watchAction(newValue,oldValue,scope): watchFn will be called when it changes

deepWatch: optional Boolean command to check whether each attribute of the monitored object has changed

$watch will return a function. If you want to log out of this watch, you can use the function

(3) Example

In the previous example, when the name form changes 30 times, an event is triggered.

The controller code is as follows:

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次了'; } }); }
Copy after login


The html code is as follows:

    
  
改变次数:{{count}}-{{name}}
Copy after login


The running effect is as follows :

You can modify it at will for the first 30 times:

Detailed explanation of $watch method in angular

After 30 modifications, the name is fixed to 'has been greater than 30 times':

Detailed explanation of $watch method in angular

This is the role of watch. Every time the model changes, the second function will be triggered.

(4)The third parameter of watch

When monitoring is an object or array, for example:

$scope.data = { name :'李四', count:20 }
Copy after login

At this time, the name and count in the data must be To monitor, you can write like this:

$scope.$watch('data',function(){ },true)
Copy after login


If you do not add the third parameter, then only data will be monitored, and it will only be triggered when the data reference changes.

So when you need to monitor some reference objects, you need to set the third parameter to true.

The above is the detailed content of Detailed explanation of $watch method in angular. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!