|
| 1 | +@ngdoc overview |
| 2 | +@name Developer Guide: HTML Compiler |
| 3 | +@description |
| 4 | + |
| 5 | +# Overview |
| 6 | + |
| 7 | +Angular's {@link api/angular.module.ng.$compile HTML compiler} allows the developer to teach the |
| 8 | +browser new HTML syntax. The compiler allows you to attach behavior to any HTML element or attribute |
| 9 | +and even create new HTML element or attributes with custom behavior. Angular calls these behavior |
| 10 | +extensions {@link api/angular.module.ng.$compileProvider.directive directives}. |
| 11 | + |
| 12 | +HTML has a lot of constructs for formatting the HTML for static documents in declarative fashion. |
| 13 | +For example if something needs to be centered, there is no need to provide instructions to the |
| 14 | +browser how the window size needs to be divided in half so that center is found, and that this |
| 15 | +center needs to be aligned with the text's center. Simply add `align="center"` attribute to any |
| 16 | +element to achieve the desired behavior. Such is the power of declarative language. |
| 17 | + |
| 18 | +But the declarative language is also limited, since it does not allow you to teach the browser new |
| 19 | +syntax. For example there is no easy way to get the browser to align the text at 1/3 the position |
| 20 | +instead of 1/2. What is needed is a way to teach browser new HTML syntax. |
| 21 | + |
| 22 | +Angular comes pre-bundled with common directives which are useful for building any app. We also |
| 23 | +expect that you will create directives that are specific to your app. These extension become a |
| 24 | +Domain Specific Language for building your application. |
| 25 | + |
| 26 | +All of this compilation takes place in the web browser; no server side or pre-compilation step is |
| 27 | +involved. |
| 28 | + |
| 29 | + |
| 30 | +# Compiler |
| 31 | + |
| 32 | +Compiler is an angular service which traverses the DOM looking for attributes. The compilation |
| 33 | +process happens into two phases. |
| 34 | + |
| 35 | + 1. **Compile:** traverse the DOM and collect all of the directives. The result is a linking |
| 36 | + function. |
| 37 | + |
| 38 | + 2. **Link:** combine the directives with a scope and produce a live view. Any changes in the |
| 39 | + scope model are reflected in the view, and any user interactions with the view are reflected |
| 40 | + in the scope model. Making the scope model a single source of truth. |
| 41 | + |
| 42 | +Some directives such {@link api/angular.module.ng.$compileProvider.directive.ng:repeat |
| 43 | +`ng-repeat`} clone DOM elements once for each item in collection. Having a compile and link phase |
| 44 | +improves performance since the cloned template only needs to be compiled once, and then linked |
| 45 | +once for each clone instance. |
| 46 | + |
| 47 | + |
| 48 | +# Directive |
| 49 | + |
| 50 | +Directive is a behavior which should be triggered when specific HTML constructs are encountered in |
| 51 | +compilation process. The directives can be placed in element names, attributes, class names, as |
| 52 | +well as comments. Here are some equivalent examples of invoking {@link |
| 53 | +api/angular.module.ng.$compileProvider.directive.ng:bind `ng-bind`} directive. |
| 54 | + |
| 55 | +<pre> |
| 56 | + <span ng-bind="exp"></span> |
| 57 | + <span class="ng-bind: exp;"></span> |
| 58 | + <ng-bind></ng-bind> |
| 59 | + <!-- directive: ng-bind exp --> |
| 60 | +</pre> |
| 61 | + |
| 62 | +Directive is just a function which executes when the compiler encounters it in the DOM. See {@link |
| 63 | +api/angular.module.ng.$compileProvider.directive directive API} for in depth documentation on how |
| 64 | +to write directives. |
| 65 | + |
| 66 | +Here is a directive which makes any element draggable. Notice the `draggable` attribute on the |
| 67 | +`<span>` element. |
| 68 | + |
| 69 | +<doc-example module="drag"> |
| 70 | + <doc-source> |
| 71 | + <script> |
| 72 | + angular.module('drag', []). |
| 73 | + directive('draggable', function($document) { |
| 74 | + var startX=0, startY=0, x = 0, y = 0; |
| 75 | + return function(scope, element, attr) { |
| 76 | + element.css({ |
| 77 | + position: 'relative', |
| 78 | + border: '1px solid red', |
| 79 | + backgroundColor: 'lightgrey', |
| 80 | + cursor: 'pointer' |
| 81 | + }); |
| 82 | + element.bind('mousedown', function(event) { |
| 83 | + startX = event.screenX - x; |
| 84 | + startY = event.screenY - y; |
| 85 | + $document.bind('mousemove', mousemove); |
| 86 | + $document.bind('mouseup', mouseup); |
| 87 | + }); |
| 88 | + |
| 89 | + function mousemove(event) { |
| 90 | + y = event.screenY - startY; |
| 91 | + x = event.screenX - startX; |
| 92 | + element.css({ |
| 93 | + top: y + 'px', |
| 94 | + left: x + 'px' |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + function mouseup() { |
| 99 | + $document.unbind('mousemove', mousemove); |
| 100 | + $document.unbind('mouseup', mouseup); |
| 101 | + } |
| 102 | + } |
| 103 | + }); |
| 104 | + </script> |
| 105 | + <span draggable>Drag ME</span> |
| 106 | + </doc-source> |
| 107 | +</doc-example> |
| 108 | + |
| 109 | + |
| 110 | +The presence of `draggable` attribute an any element gives the element new behavior. The beauty of |
| 111 | +this approach is that we have thought the browser a new trick, we have extended the vocabulary of |
| 112 | +what browser understands in a way, which is natural to anyone who is familiar with HTML |
| 113 | +principles. |
| 114 | + |
| 115 | + |
| 116 | +# Understanding View |
| 117 | + |
| 118 | +There are many templating systems out there. Most of them consume a static string template and |
| 119 | +combine it with data, resulting in a new string. The resulting text is then `innerHTML`ed into |
| 120 | +an element. |
| 121 | + |
| 122 | +<img src="img/One_Way_Data_Binding.png"> |
| 123 | + |
| 124 | +This means that any changes to the data, need to be re-merged with the template and then |
| 125 | +`innerHTML`ed into the DOM. Some of the issues are: reading user input and merging it with data, |
| 126 | +clobbering user input by overwriting it, managing the whole update process, and lack of behavior |
| 127 | +expressiveness. |
| 128 | + |
| 129 | +Angular is different. Angular compiler consumes DOM with directives, not string templates. The |
| 130 | +result is a linking function, which when combined with a scope model results in live view. The |
| 131 | +view and scope model bindings are transparent, no action from the developer is needed to update |
| 132 | +the view. And because no `innerHTML` is used there are no issues of clobbering user input. |
| 133 | +Furthermore, angular directives can contain not just text bindings, but behavioral constructs as |
| 134 | +well. |
| 135 | + |
| 136 | +<img src="img/Two_Way_Data_Binding.png"> |
| 137 | + |
| 138 | +The Angular approach produces stable DOM. This means that the DOM element instance bound to model |
| 139 | +item instance does not change for the lifetime of the binding. This means that the code can get |
| 140 | +hold of the elements and register event handlers and know that the reference will not be destroyed |
| 141 | +by template data merge. |
| 142 | + |
| 143 | + |
0 commit comments