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

105
Views
Inherit attributes in component definition

I have a basic image component and within it there is an img element. Is there a way to pass the native img attributes to image without having to define them all, such as alt and have them all applied to the img tag?

Here is some pseudo code of what I mean:

@Component({
  selector: 'image',
  template: '<img [src]="src" [...attrs]>'
})
export class ImageComponent {
  @Input() attrs;
}
<image img.alt="Some Alternative Value">
  <another-component></another-component>
</image>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Yeah, this can be done (if it's what you're after ..)

@Component({
  selector: 'image',
  template: '<img #imgRef [src]="src">'
})
export class ImageComponent {
  @ViewChild('imgRef') imgRef: ElementRef<HtmlElement>;
  @Input() attrs;

  ngAfterViewInit() {
    // assuming attrs is a key/value object
    Object.entries(this.attrs).forEach(([key, value]) => {
     this.imgRef.nativeElement.setAttribute(....)
    })
  }
}
about 4 years ago · Juan Pablo Isaza Report

0

As far as I'm aware of, it is not possible to reflect attributes from the component host into the component view, without having to manually define @Input()s for every possible attributes.

An possible alternative, if it applies to you, is to project an img element from outside your image component, inside the component's view.

<image>
  <img alt="Some Alternative Value">
  <another-component></another-component>
</image>

That way, you basically have the img element exposed, where you can freely add attributes, among others.

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!