当前位置:主页 > 网页前端 > AngularJS >

Angular4实现动态添加删除表单输入框功能

时间:2020-12-02 13:06:46 | 栏目:AngularJS | 点击:

首先介绍一下实现的效果,就是单一表单能实现添加其他的,也能删除

代码如下:

<h5>动态添加表单</h5> 
<div class="form"> 
 <div class="form-group form-group-sm" *ngFor="let i of login"> 
  <label class="col-form-label">用户名</label> 
  <input class="form-control" [(ngModel)]="i.username" value="{{i.username}}"> 
  <label class="col-form-label">密码</label> 
  <input class="form-control" [(ngModel)]="i.password" value="{{i.password}}"> 
  <button class="btn btn-link" (click)="removeInput(i)">删除</button> 
 </div> 
 <button (click)="addInput()">增加</button> 
 <button (dblclick)="dbclick()">双击</button> 
 {{ login | json }} 
</div> 
username: any; 
 id: number = 1; 
 login:any = [{"username": "username" + this.id,"password": "pwd" + this.id}]; 
 addInput() { 
  console.log('点击'); 
  console.log(this.login); 
  let number = this.login.length + 1; 
  this.login.push({"username": "username" + number, "password": "pwd" + number}); 
  console.log(this.login); 
 } 
 removeInput(item) { 
  console.log(item); 
  let i = this.login.indexOf(item); 
  console.log(i); 
  this.login.splice(i, 1); 
 } 

总结

您可能感兴趣的文章:

相关文章