Angularjs Advanced (32) the special NG-SRC and Ng-href of the books of the Sea

Source: Internet
Author: User

The special NG-SRC and ng-href of the sea

Before explaining the special two instructions, you need to understand the start and execution of Ng, as follows:

1) The browser loads the static HTML file and parses it into DOM;

2) Browser loading angular.js file;

3) Angular Monitoring domcontentloaded event, monitoring to start;

4) Angular look for Ng-app directive, determine the scope of action;

5) Find the module defined in the app using the $injector service for dependency injection;

6) Create $compile service for compiling according to $injector service;

7) $compile Services to compile instructions, filters, etc. in the DOM;

8) Use the NG-INIT directive to replace variables in the scope;

9) Finally, our final view is generated.

As you can see, the NG framework starts to function after the domcontent is loaded. If we have a picture in the template as follows:

The page will always display an incorrect picture until the page starts loading until NG compiles, because the path {{Imgurl}} has not been replaced, like this:

To avoid this, we use the NG-SRC directive so that no images can be found until the path is correctly obtained. Similarly, the href attribute of the,<a> tag needs to be replaced with ng-href, so there is no link to the wrong address on the page first.

Let's think a little more about that, when we use {{}} in the template to display the data, will it not show curly braces and expressions on the page before the NG compilation is complete? That's true. To avoid this, NG has a directive equivalent to {{}}: Ng-bind, which is also used for one-way bindings, does not display data that is useless to the user when the page is just loaded. In spite of this you may not only not comfortable but more tangled, {{}} so easy to understand, still can not use it? The good news is that we can still use it. Because I am writing a single-page application, the page will only be loaded index.html when the problem, just the template in the index.html to replace the Ng-bind on the line. The other templates are dynamically loaded, so we can safely use {{}}.

Angularjs Advanced (32) the special NG-SRC and Ng-href of the books of the Sea

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.