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