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

307
Visualizações
onLoad happening multiple times with Iframe

Having some issues with Angular 2, im making a component that has a iframe and sets the source from the input. However when it loads, it hits onLoad twice, one with src='' and another with the actual input url.

I cant seem to figure out how to hook the url up for the iframe when it gets put into the view.

export class ExternalComponent implements OnInit {
    @Input()
    url: string;

    private src: any;

    constructor(private sanitizer: DomSanitizer) {

    }

    ngOnInit()    
    {
        this.src = this.sanitizer.bypassSecurityTrustResourceUrl(this.url)
    }

    onLoad() {

    }
}

Html:

<iframe [src]="src" frameBorder="0" (load)="onLoad()"></iframe>

Reproduction: http://plnkr.co/edit/Dnpmv6X2IO3WGQAg0372?p=preview

Thanks

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You could try an attribute binding, which isn't added at all if the value is null

<iframe [attr.src]="src ? src = null" frameBorder="0" (load)="onLoad()"></iframe>

update

As explained in https://stackoverflow.com/a/15880489/217408 when the event handler is added to the element before the <iframe> is added to the DOM. This seems to be what Angular is doing.

Add event handler imperatively after the element was added to the DOM

Plunker example

Ignore events while url is null

Plunker example

about 4 years ago · Santiago Trujillo Relatório

0

ngOnInit()    
    {
        this.src = this.sanitizer.bypassSecurityTrustResourceUrl(this.url)
    } 
This is wrong code if use like this iframe will load more time .
ts file 
ngOnInit()    
    {
        this.src = this.url;
    } 
make html page like this (use pipe)
<iframe [src]="src| safe" ></iframe>

pipe file

      import { DomSanitizer} from '@angular/platform-browser';
    constructor(private sanitizer:DomSanitizer){
      return   this.sanitizer.bypassSecurityTrustResourceUrl(value);
about 4 years ago · Santiago Trujillo Relatório

0

Using debugger I found out that event.target.src could provide the solution. The first time it runs, it is an empty string "". The second time the value is the image url. So, in my case it works like that:

onLoad(event: any) {
  if (event.target.src) {
      //do some stuff here
  }
}
about 4 years ago · Santiago Trujillo 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