Angular $apply () and $digest () Tutorial 1

Source: Internet
Author: User

Some well-known criticisms and flaws. They all involve a very common problem in the $digest loop (update cycle): How do I update $scope outside of angular?

Where do you call $apply?

It is a better practice to make sure that you are calling $apply outside of the $digest loop, such as in the "link" Directive (Directive) response function.

That is, you can only call $apply outside of $controller, so that it can access the declaration code of HTML or other controller controllers-for example, Apply invokes the link directive and triggers the corresponding function, which is certainly outside of the $digest loop .

How do I handle errors that are thrown outside of Angularjs?

$scope. $apply () receives a function or angular expression string, executes it, and then calls $scope. $digest () to update all bindings or listeners.

If your code is not wrapped in a function passed to $apply, it throws an error, and the error is thrown outside of Angularjs, which means that the error handling mechanism in your app cannot handle it. $apply not only executes your code, but its internal try/catch statement keeps your error captured, and $digist is the last guarantee that it will work even if a bug is thrown. That's pretty good.

Therefore, we should change the expression beyond angular and update $scope within the $apply.

Summarize

I hope you now know when and how to use $apply (). If you only need ANGULARJS to provide you with the functionality, then the probability you use it is not high. However, if you write a custom directive and manipulate the DOM element directly , it is necessary to understand it.

Angular $apply () and $digest () Tutorial 1

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.