Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
Wanted to Update Title and Image in Angular after component load

Wanted to update Dom elements like Image and Text after loading component in Angular. Note: Both of them does not have ID but have Class Name.

I used following code:

@ViewChild('.appnametitle') el: ElementRef;
  constructor(
    private renderer: Renderer2,
    @Inject(DOCUMENT) private document,
    @Inject(ONEPLACE_JS_URI) private oneplaceJsUri,
    public cookieConfig: CookieConfig
  ) { }

  ngAfterViewInit(): void {
    this.renderer.setAttribute(this.el.nativeElement, 'innerHTML', 'Test Title Change');
  }

but it shows following error: Cannot read properties of undefined (reading 'nativeElement')

Could you help me where I am doing it wrong?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

ViewChild has not a classname as selector (a directive yes). So you can create a directive with the selector of the class and use ViewChild asking about the directive

@Directive({
  selector: '.appnametitle'
})
export class SelectDirective {

  constructor(public el:ElementRef) { }

}

And in Component:

@ViewChild(SelectDirective) selectDirective:SelectDirective

ngAfterViewInit(): void {
    this.renderer.setAttribute(this.selectDirective.el.nativeElement,
              'innerHTML', 'Test Title Change');
  }

But this is used if you has not access to the component (because was on third persons, e.g.), else you should rethinking the problem in a more angular way

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!