Angular $ watch

Source: Internet
Author: User

Among all the built-in functions in scope, the $ watch function is used most often. When a part of your data model changes, the $ watch function can send you a notification. You can monitor the attributes of a single object, or monitor the results (functions) that need to be computed. In fact, as long as they can be accessed as attributes, or they can be computed as a JavaScript function, it can be monitored by the $ watch function. The function signature is $ Watch (watchfn, watchaction, deepwatch)

The meanings of each parameter are as follows.
Watchfn

This parameter is a string with an angular expression or function. It returns the current value of the monitored data model. This expression will be executed many times, so make sure it does not produce any other side effects. That is to say, make sure that it can be called many times without changing the status. For the same reason, the monitoring expression should be easily computed. If you use a string to pass an angular expression, it will be executed for the objects in the scope that calls it.
Watchaction

This is a function or expression that is called when watchfn changes. If it is in the form of a function, it will receive two new and new values of watchfn, and the reference of the scope object. The function signature is function (newvalue, oldvalue, scope ).
Deepwatch

If it is set to true, this optional Boolean parameter will command angular to check whether each attribute of the Monitored object has changed. You can use this parameter if you want to monitor elements in an array or all attributes of an object, instead of simply monitoring a simple value. Angular needs to traverse arrays or objects. If the set is large, the computing burden will be heavy.

$ Watch function returns a function. when you no longer need to receive a change notification, you can use this function to log out of the monitor.

If we need to monitor a property and then cancel monitoring, we can use the following code:
...
VaR dereg = $ scope. $ Watch ('somemodel. someproperties', callbackonchange ());
...
Dereg ();

Let's go back to the shopping cart case in Chapter 1 and complete its functions. For example, if the value of the item added to the shopping cart is more than 100 USD, we will give him a 10 USD discount. We will use the following template:
<Div ng-controller = "cartcontroller">
<Div ng-repeat = "item in items">
<Span >{{ item. Title }}</span>
<Input ng-model = "item. Quantity">
<Span >{{ item. Price | currency }}</span>
<Span >{{ item. Price * item. Quantity | currency }}</span>
</Div>
<Div> total :{{ totalcart () | currency }}</div>
<Div> discount: {bill. Discount | currency }}</div>
<Div> subtotal :{{ subtotal () | currency }}</div>
</Div>

The cartcontroller may look like the following:
Function cartcontroller ($ scope ){
$ Scope. Bill = {};
$ Scope. Items = [
{Title: 'paint pots', quantity: 8, price: 3.95 },
{Title: 'polka dots', quantity: 17, price: 12.95 },
{Title: 'pebbles ', quantity: 5, price: 6.95}
];
$ Scope. totalcart = function (){
VaR Total = 0;
For (VAR I = 0, Len = $ scope. Items. length; I <Len; I ++ ){
Total = total + $ scope. items [I]. Price * $ scope. items [I]. quantity;
}
Return total;
}
$ Scope. Subtotal = function (){
Return $ scope. totalcart ()-$ scope. discount;
};
Function calculatediscount (newvalue, oldvalue, scope ){
$ Scope. Bill. Discount = maid> 100? 10: 0;
}
$ Scope. $ Watch ($ scope. totalcart, calculatediscount );
}

Note that at the bottom of cartcontroller, we set a metric on the value of totalcart () to calculate the total price of this purchase. As long as this value changes, the monitor will call calculatediscount (), and then we can set the discount to the corresponding value. If the total price exceeds $100, we will set the discount to $10. Otherwise, the discount is 0.

 

Angular

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.