Update: I put a bounty on this question. I am not looking for hacks or workarounds. I am looking for an official way to access the dom in an angular component, and an explanation why the behavior I see ($postLink running to early) seems to be contradictory to the official docs.
The official docs state (here):
$postLink() - Called after this controller's element and its children have been linked. Similar to the post-link function this hook can be used to set up DOM event handlers and do direct DOM manipulation
Original question: I have an example of the problem here -> http://plnkr.co/edit/rMm9FOwImFRziNG4o0sg?p=preview
I am using an angular component and I want to modify the dom in the post link function, but it doesn't work, it seems that the function runs too early, before the template is actually ready in the dom after all the angular processing.
In the html page, I have this:
<my-grid grid-id='foo'></my-grid>
The component is defined as:
appModule.component('myGrid',{
controller: gridController,
bindings: {
gridId: <,
},
templateUrl: 'gridTemplate'
});
In the component template I have this:
<table id='{{$ctrl.gridId}}'>
...
(The binding itself works, there is no doubt. Eventually, in the html the id of the table is 'foo' as expected).
In the controller, I have something like this:
function gridController($scope, $compile, $attrs) {
console.log (grid id is: + this.gridId); // 'foo'
this.$postLink = function() {
var elem = document.getElementById(this.gridId);
// do something with elem, but elem is null
}
}
What I see when debugging is that when the $postLink function is executed, the table is in the dom but its id attribute is still {{$ctrl.gridId}}
instead of foo
, so document.getElementById()
finds nothing. This seems in contrast to the documentation.
What am I missing? Is there a different way to access the dom in the component?
Update 2: Today I realized the same problem occurs with the regular link function of directives, it is not limited to components. So apparently I misunderstood the meaning of do direct DOM manipulation - the link function runs on an element that is detached from the dom, so using the document
object with selectors is useless.