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

247
Vistas
Angular 2: Generate random int throws error

I have a list where I am using a random number to choose an avatar and its throwing the following error:

HelloIonicPage.html:33 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.

View:

  <ion-list>
    <ion-item *ngFor="let client of clients; let i = index;>
      <ion-avatar item-left>
        <img [src]="getRandomInt(0, 9)">
      </ion-avatar>
    </ion-item>
  </ion-list>

.ts

getRandomInt(min, max) {
    let num =  Math.floor(Math.random() * (max - min + 1)) + min;
    return "https://randomuser.me/api/portraits/lego/" + num + ".jpg"
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I guess you can try trick with background-image and [ngStyle]

HTML

<ion-list>
  <ion-item *ngFor="let client of clients; let i = index; let r = getRa" >
    <ion-avatar item-left>
     <div class="avatar-photo" [ngStyle]="{ 'background-image': 'url(' + getRandomInt(0, 9) + ')' }"></div>
    </ion-avatar>
  </ion-item>
</ion-list>

You also need additional CSS, so it custom, change as you need

.avatar-photo {
    width: 160px;
    height: 160px;
    border-radius: 50%;
    background-size: contain;
    background-repeat: no-repeat;
}

I hope it help!

over 4 years ago · Santiago Trujillo 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