Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda