Use of $watch and $observe

Source: Internet
Author: User

$observe is a method of the attribute object that listens for changes in the property values in the DOM. For example attr1= "{{name}}"

attribute is defined on the third parameter of the link function in directive. So $observe can only be used in directive.

You can see that its callback function has only one parameter, which is the new value.

    function () {        return  {            function(scope, element, attrs) {                attrs. $observe (  function(newval) {                    //....                 })            }        }    })

Summarized as follows:

$observe can only be used in directive, $watch not so many restrictions, $watch is the method on the scope object, so it can be used in the Controller or directive link method.

$observe can only be monitored for changes in DOM property values, $watch can listen to an expression, which can be a function or a string. If it is a string, it is converted to a function using $parse. String expression cannot contain {{}}.

$observe callback functions are newvalue, $watch have oldvalue and newvalue

Use of $watch and $observe

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.