Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
¿Cómo muestro un cambio de índice en ngFor (Angular)?

Tengo una función llamada removePopUp. Cuando envío un índice, a veces quiero cambiar lo que hay dentro de ese índice en la matriz. Digamos que tengo una matriz de [0,1,2]. cuando ejecuto removePopUp(1) que debería devolver [0,2], no debería haber un tercer elemento, pero el tercer índice se deslizaría hacia el segundo elemento eliminando ese elemento. El problema es que no muestra esta actividad en angularJs cuando estoy usando un ngFor para mostrar un componente secundario.

mensajes.componente.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 } }

mensajes.componente.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 answers
Answer question

0

Si necesita eliminar el elemento n-th de una matriz, puede usar un splice como este:

 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); }

Nota: es una buena idea verificar si el índice proporcionado es un número y también si su valor está dentro de la longitud de la matriz. Tenga en cuenta que proporcionar un valor negativo para el splice comenzará a buscar desde el final de la matriz, por lo que ambas comprobaciones son necesarias.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!