且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Angular和Ionic,如何在单击按钮时将元素添加到DOM

更新时间:2023-08-25 16:20:46

如果我正确解释了您想要的内容,它应该是这样.除非您在其他地方使用了 id name 属性,否则它们就不需要存在.

This is what it should look like if I am interpreting what you want correctly. Your id and name attributes of an emailinput don't need to be there unless you have some use for them somewhere else.

<ion-content>

  <ion-item id="row" *ngFor="let emailinput of emailinputs ; let i = index">
    <ion-label>
      Email
    </ion-label>
    <ion-input type="email" id="email{{i}}" placeholder="jdoe@gmail.com" (keyup.enter)="Send($event.target.value)" [(ngModel)]="emailinput.email"></ion-input>
  </ion-item>

  <div padding>
    <button (click) = "addnewemail()" id="register" ion-button full color="nudgetprim" block>Add</button>
    <button (click) = "sendinvite(emailVal)" id="sendinvite" ion-button full color="nudgetprim" block>Invite</button>
  </div>

</ion-content>


emailinputs = [{'id' : 'row0', 'name' : 'row0', 'email': ''}];

...

constructor(...

...

addnewemail() {
    let newItem = this.emailinputs.length;
    this.emailinputs.push({'id' : 'row' + newItem, 'name' : 'row' + newItem, 'email': ''});
}