Win a copy of Kotlin Cookbook this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

How to select a component's HTML element inside a decorator function

 
Saloon Keeper
Posts: 2898
370
Android Eclipse IDE Angular Framework TypeScript MySQL Database Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am experimenting with decorators in Typescript/Angular, try to decorate a component with the text for the title of the component's template.

My problem is that I can not figure-out how to properly select the component's HTML element.  This is what I have so far - the result of the document.querySelector always returns undefined:

app.component.html
app.component.ts
 
Ron McLeod
Saloon Keeper
Posts: 2898
370
Android Eclipse IDE Angular Framework TypeScript MySQL Database Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am thinking that the issue with code is that the at the time when the decorator executes is too early on in the component lifecycle and the component's template has not yet been injected in to the DOM, so it isn't found.
 
Ron McLeod
Saloon Keeper
Posts: 2898
370
Android Eclipse IDE Angular Framework TypeScript MySQL Database Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Ron McLeod
Saloon Keeper
Posts: 2898
370
Android Eclipse IDE Angular Framework TypeScript MySQL Database Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a working solution by wrapping the ngOnInit() function so that the title was set after the component had already been initialized and the template had been applied:

 
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!