"Turn" angular study notes (14)-$watch (1)

Source: Internet
Author: User

This article mainly introduces the basic concepts of $watch:

$watch is the method built into the $scope of all controllers:

$scope. $watch (Watchobj,watchcallback,ifdeep)

Watchobj:

The object that needs to be detected can be any of the following:

1. A data to monitor whether the value of this data has changed

2. A angular expression that monitors whether the results of an expression change

3. function () to monitor whether the function's return value has changed

Note that all of the above three, either, should be in string format and are executed under the $scope scope.

4. function, not string format, but directly into a function, you can directly write an anonymous function, you can also pass in a function, note that it is not under the $scope scope, so, if the current scope of the function is passed, still need to write: $scope. Fun

Watchcallback:

Accepts a function or expression that is called or executed when a watchobj change occurs.

If it is in the form of a function, it will receive three parameters:

Watchcallback (Newvalue,oldvalue,scope)

The new value of the Newvalue:watchobj

The old value of the Oldvalue:watchobj

Scope: Is the $scope of the current controller

Note: A function or expression is not executed under the $scope scope, so if you need to call a function under the current scope, you should $scope.watchcallback

Ifdeep:

A Boolean value

If the Watchobj type is an object or an array, the Ifdeep value is set to True, then angular detects whether each property of the monitored object has changed, not just a simple value.

Finally, $ (watch) returns a function that can be used to destroy the controller, which is called only once:

var destroy = $scope. $watch (...);

Destroy ()

Here's a very basic example:

<! DOCTYPE html>
function Watch ($scope) {    $scope. Number = 0;    $scope. Count = function (newvalue,oldvalue,scope) {        $scope. result = $scope. number*3;        Console.log (newvalue);        Console.log (oldValue);        Console.log (scope);        return $scope. Result    };    $scope. $watch (' number ', $scope. Count,false)}

We monitored the number, which, when numbers changed, called the Count function, changing the value of result.

Where the three parameters in count accept the new value of number, the old value, and the $scope of the current controller, respectively:

For example, I enter 3 in the text field:

--------------------------------------------------------------------------------------------------------------- ---------------------

Legacy issues:

When a function is passed in the first parameter of $watch, it can be monitored even if the function does not have a return value. So, I don't know what it is that is monitoring what's changing.

An example can refer to the next note http://www.cnblogs.com/liulangmao/p/3723385.html

From:http://www.cnblogs.com/liulangmao/p/3722885.html

"Turn" angular study notes (14)-$watch (1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.