I'm trying to get a better understanding how AngularJS compiles directives and updates the view.
Here's my specific problem. I have a directive like so:
angular.module('directives', [])
.directive('test', function() {
return {
restrict: 'AE',
scope : { foo : '=' },
template: '<div id={{foo.id}}>{{foo.data}}</div>'
link: function(scope, element, attrs) {
document.getElementById(scope.foo.id); /*1*/
}
}
});
At point 1 scope.foo.id is defined. However, the template for the directive has not been compiled yet, so the div id is not set, and getElementById returns null. Once the page is completely rendered, and I look at the compiled template, all of the template id's have been successfully set to foo.id
What am I missing here?
It's also important to note that for my particular case I need to explicitly interact with the template div by it's id.
EDIT: added a fiddle : http://jsfiddle.net/6c4nb/8/