Specifically, I'm wondering how they update elements without using innerHTML. In the docs, they clearly state how they're better than other templating engines because they don't re-render with innerHTML (ctrl-f innerHTML -- sorry). I started poking through the source code but there's a LOT of it and was hoping that perhaps I could get a faster answer from you guys.
So far my guesses have been
- The compiler converts
{{test}}
into something like<ng-bind>test</ng-bind>
which the linker can update when data changes, but it doesn't seem like this is happening when I look at the DOM of a rendered Angular page. And also seems like it could interfere with a client's CSS and other javascript components external to Angular. - They actually are using innerHTML but they're just not re-rendering the ENTIRE DOM -- only a small snippet of it (perhaps the data inside some sort of custom
<ng-bind></ng-bind>
tag). - They're somehow removing and appending new elements ... bad guess I think.
If anyone knows I'd love to learn. Otherwise it's back to the source code for me.
EDIT New Guess
After thinking about it some more, I believe this might be what happens: Compiler swallows html, say something like
<p>
{{model}}
<div>
<p> Hello ! </p>
</div>
</p>
And converts it to this:
<p class="ng-binding">
{{model}}
<div>
<p> Hello ! </p>
</div>
</p>
Then Angular can crawl through and index all angular text nodes ( {{model}}
) eg document.getElementsByClass('ng-binding')[0].childNodes[0]
. Each stored node can then be associated by the linker with a scoped model $scope['model']
. Each node can then be updated extremely quickly by setting node.nodeValue = $scope['somemodel
] (simplified)` and voilà, technically no innerHTML'ing and lightning speed DOM updates.