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

224
Visualizações
How do I display an index change in ngFor (Angular)

I have a function called removePopUp. When I send an index sometimes I want to change whats inside that index in the array. Lets say I have an array of [0,1,2]. when I run removePopUp(1) that should return [0,2] there should be no 3rd element but the 3rd index would slide to the second element removing that element. The issue is its not displaying this activity in angularJs when I'm using a ngFor to display a child component.

messages.component.ts

...
userPopUpsArray : BookFaceMessage[] = []
...
removePopUp(index){
    index = Number(index)
  let usersLength = this.userPopUpsArray.length
if (usersLength === 3){
  if (index === 1){
    let thrid_index = this.userPopUpsArray[2]
    this.userPopUpsArray[1] = thrid_index
  }
  else if(index === 0){
    let second_index = this.userPopUpsArray[1]
    let thrid_index = this.userPopUpsArray[2]
    this.userPopUpsArray[0] = second_index
    this.userPopUpsArray[1] = thrid_index
    this.userPopUpsArray.pop()

  }
  else{
    console.log(index)

    this.userPopUpsArray.pop()
  console.log(this.userPopUpsArray)
  }
}
else if (usersLength === 2){
  
  if(index === 0){
    this.userPopUpsArray[0] = this.userPopUpsArray[1]
    this.userPopUpsArray.pop()

  }
  else{
    console.log(index)

    this.userPopUpsArray.pop()
  console.log(this.userPopUpsArray)
  }
}
else{
  this.userPopUpsArray.pop()
}
getPopUp(){
  return this.userPopUpsArray
}
}

messages.component.html

...
 <div *ngFor="let userPopUpData of userPopUpsArray; let i = index;trackBy: getPopUp">
      <app-message-popup [userPopUpsArray]=userPopUpsArray [userPopUpData]=userPopUpData [removePopUp]=removePopUp [index]=i></app-message-popup>
...
</div>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you need to remove the n-th element of an array you can use splice like this:

  removePopUp(index) {
    index = Number(index);

    // check if provided index is number
    if (isNaN(index)) {
      return;
    }

    // check if provided index is within array length
    if (index < 0 || this.userPopUpsArray.length <= index) {
      return;
    }

    this.userPopUpsArray.splice(index, 1);
  }

Note: it is a good idea to check if provided index is number, and also if its value is within array length. Keep in mind providing negative value to splice will start looking from the end of the array so both checks are necessary.

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