Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda