1

I have a little form like this whenever I click "add" a new row gets added.

HTML:

   <div class="tel" *ngFor="let t of tel; let i = index">
          <div class="row">
            <div class="col-md-2">
              <input type="text" class="form-control" [(ngModel)]="t.telFijo" [name]="'tfijo'+i">
            </div>
            <div class="col-md-2">
              <input type="text" class="form-control" [(ngModel)]="t.telCelular" [name]="'tcel' + i">
            </div>
            <div class="col-md-2">
              <input type="text" class="form-control" [(ngModel)]="t.anexo" [name]="'anex' + i">
            </div>
            <div class="col-md-2">
              <input type="text" class="form-control" [(ngModel)]="t.skype" [name]="'skype' + i">
            </div>
            <div id="em" class="col-md-3">
              <input type="email" class="form-control" [(ngModel)]="t.email" [name]="'email' + i">
            </div>
            <div class="col-md-1">
              <i id="trash" class="fa fa-trash" (click)="eliminar(i)"></i>
            </div>
          </div>
        </div>

Whenever I add I push an object.

  agregar() {
 const telefono = new Telefono();
this.tel.push(telefono);
}

And whenever I remove an item I splice it in the current position.

 eliminar(i) {
 this.tel.splice(i, 1);
 }

That works fine, the issue is that when I try to add a new row after I deleted one, the last two rows get deleted.

In this example I had 3 rows, deleted the middle one and then added one. Which made the previous one get deleted. I honestly have no idea why this happens. I would love to know if there is something I'm missing.

1 Answer 1

1

In addition

agregar() {
  const telefono = new Telefono();
  this.tel.push(telefono);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I had telefono as a global variable that was also a new Telefono(); and it didn't work, just for clarification declaring it inside the method is also not working :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.