I believe that this question is a continuation of your another question Why I can't access template DOM element in the constructor of the component. There you had the following:
export class AppComponent {
constructor(){
button : HTMLElement = document.querySelector('button');
refreshClickStream$ = Observable.fromEvent(this.button, 'click').subscribe();
}
Console output is null here, because the button in DOM is not created
yet, right?
And you are right about that as I explained in the mentioned answer.
But why I can use its clicked() function if it is not created yet..?
It's because Angular when constructing view nodes for the AppComponent
will bind to the click
event of the button. Since Angular creates these DOM elements it knows when it has to subscribe.
This all happens inside createViewNodes function:
function createViewNodes(view) {
for (var i = 0; i < def.nodes.length; i++) {
var nodeDef = def.nodes[i];
switch (nodeDef.flags) {
case 1 /* TypeElement */:
var el = (createElement(view, renderHost, nodeDef));
...
listenToElementOutputs(view, componentView, nodeDef, el); <--------------