Why I can't access the function

19 views
Skip to first unread message

Frank Mehlhop

unread,
Mar 19, 2018, 8:09:07 AM3/19/18
to Angular and AngularJS discussion
Hi,

I got a angular application created by cli.
At a componant.ts I got this code inside the constructor:
function getLangSelector() {
return document.getElementById('getLangSelector() !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
}

const e = document.getElementById('langSelector');
if (e === undefined) {
console.log('Can\'t find langSelector !!!!!!!!!!!!!!!!');
} else {
if (getLangSelector() === null) {
console.log('getLangSelector() === null !!!!!!!!!!!!!!!!');
} else {
console.log('getLangSelector() !== null !!!!!!!!!!!!!!!!');
}
}

the only log I get at the console is:
"getLangSelector() === null !!!!!!!!!!!!!!!!"

Why I don't get the log "getLangSelector() !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 
from the function getLangSelector()?
What I need to change to get it work?

Frank

Sander Elias

unread,
Mar 20, 2018, 12:29:23 AM3/20/18
to Angular and AngularJS discussion
Hi Frank,

The constructor does run before the template is added. so it will alway's return null. BTW, even after the initialization of the template, your function would return null. My guess is you don't have an element with id "getLangSelector() !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!".

Regards
Sander
Message has been deleted
Message has been deleted

Frank Mehlhop

unread,
Mar 20, 2018, 5:19:47 AM3/20/18
to Angular and AngularJS discussion
Thanks Sander for your lines!
I'm happy you spend your time for this problem.
You are right, the id is different.

Nevertheless I found out, that I can get the element, when I ask for in the ngOnInit-Method.

Cheers, Frank
Reply all
Reply to author
Forward
0 new messages