First, the overall
Angular 's HTML compiler allows developers to customize the new HTML syntax. compiler allows us to have arbitrary HTML elements or attributes, even new html tags, attributes (such as <beautiful girl="CF" ></beautiful >) additional behavior. Angular These additional behaviors as directives.
HTML has a number of predefined HTML style structures that specifically format static documents (you can tell the browser how to display the tagged content). Suppose something needs to be centered, and we don't need to teach the browser how to do it (omit the N word here). We just need to simply add the align="Center" to the label that needs to be centered. This is where thelanguageof the declarative language (declarative- X ) is .
But declarative language also has its limitations, that is, you can't tell the browser how to handle syntax outside of a predefined range. For example, we can't simply tell the browser how to make the text snap to the 1/3 of the browser . So, we're going to need a way to keep the browser up-to-date and learn new grammar.
Angular pre-binds some directives that help build your app . We can also create our own unique directivesfor our own applications. These directive Extensions will become "domain-specific languages" (Domain specific Language) for our own applications.
These compilations will only occur on the browser side, without the service side or pre-compilation steps.
Second, Compiler
Compiler, as a servicefor Angular , is responsible for traversing the DOM structure and looking for properties. The compilation process is divided into two stages:
1. Compile (Compile): Traverse the DOM node tree to collect all directives. The return result is a link function (linkingfunctions).
2. Link (link ): directives bound to a scope ( scope live view ). Any changes in scope model scope model To reflect the correct value.
Some directives, such as ng-repeat, will be for each in the set (collection ) to copy a specific element (group) at a time. Compile and link the two phases to improve performance. Because the cloned template (template) only needs to be compiled once, and then a link (similar to the template cache) is made to the elements in each collection.
Third, Directive
Directive is a behavior that encounters a specific during compilation html structure, it will be triggered. can be placed in the element's , attribute class even comments. The following are several references to ng-bind (a built-in ) method:
<span ng-bind= "exp" ></span><span class= "NG-BIND:EXP;" ></span><ng-bind></ng-bind><!--directive:ng-bind Exp---
Directive is just a function that executeswhen the compiler encounters it in the DOM . the Directive API documentation explains in detail how to create a directive. Here's a sample that lets an element play hide and seek with your mouse ...
<! doctype html> Adding the "Wildcat" attribute to any element will cause the element to have a new behavior. In this way, we teach the browser how to deal with the elements that will hide and seek (rest assured, you are not in a room, you will not hang -_-! ). We have expanded our browser's "vocabulary" through this approach. For anyone familiar with HTML rules, this is a more natural way
Iv. Understanding Views (view)
There are many template systems outside, and they are usually connected by a template string to the data, generating the final HTML string and writing the result to an element through the innerHTML attribute.
This means that when any data changes, you need to re-merge the data, the template into a string, and then act as a innerHTMLwrite back to the corresponding element. There are some problems here:(I can't understand the literal translation here ...only aYY)Suppose there is a scenario where the template contains an input box. The user enters the input box and updates the template synchronously. Normal template throughInnerHTML, strings, and data connections to update the view, which interrupts the user's input and experiences a bad experience. Angularis different. AngularCompiler (compiler) bydirectivesprocessingDOM, rather than by processing a string template. The processing result is aScope Modelgroup Merge to generate a link function for the live template (linking function). View andScope Modelthe bindings are transparent to us. Developers do not need to update views,Modeldo any action. And, because there is no useInnerHTMLupdate the view template, so user input is not interrupted. In addition,Angular Directivesnot only can you bind literal values, but it can also be the structure that owns the behavior (Behavioral constructs).
This approach to Angular produces a stable DOM. This means that in the life cycle of a DOM element, it is bound to an instance of a model , and the relationship does not change. This also means that the code can keep a reference to a DOM object, register the event function with it, and the reference will not be destroyed by the template data merge.
ANGULARJS Study notes--html compiler