如何在Angular 8中动态添加一对文本输入和textarea?

问题描述 投票:0回答:2

下面这段html来自于我们的angular页面,在这个页面中,用户应该介绍一个问题和相应的答案(类似Q&A的东西)。

 <mat-card>
           <button mat-stroked-button color="primary" style="margin-right: 20px;">
               Add a new Q & A pair
            </button>
            </mat-card>
            <div *ngFor="let qna of interview.qnas">
                <mat-card id="{{qna.id}}" style="margin-bottom: 25px;">
                    <mat-form-field>
                        <input matInput type="text" class="form-control" placeholder="Interview question" required [(ngModel)]="qna.question" name="question">
                    </mat-form-field> 
                    <mat-form-field>
                        <textarea matInput rows="5" class="form-control" placeholder="Ideal answer from the talent" [(ngModel)]="qna.answer" name="answer"></textarea>
                    </mat-form-field>
                    <button mat-stroked-button color="warn">
                        Remove this
                    </button>
                </mat-card> 
            </div>

在html中可以看到,里面有两个按钮,第一个按钮应该是让用户添加一个新的对和,第二个按钮应该是删除一个对。我大概可以想出一个删除对子的方案,但我不太清楚如何实现在用户点击时飞快地添加一个新的Q&A对子。另外,请考虑我们也有双向绑定的功能。

angular typescript angular-material angular-components
2个回答
1
投票
<mat-card>
    <button mat-stroked-button color="primary" style="margin-right: 20px;"
            (click)="addQnApair()">
        Add a new Q & A pair
    </button>
</mat-card>
<div *ngFor="let qna of interview.qnas; let i= index">
    <mat-card id="{{qna.id}}" style="margin-bottom: 25px;">
        <mat-form-field>
            <input matInput type="text" class="form-control"
                   placeholder="Interview question" required [(ngModel)]="qna.question"
                   name="question">
        </mat-form-field>
        <mat-form-field>
                        <textarea matInput rows="5" class="form-control"
                                  placeholder="Ideal answer from the talent" [(ngModel)]="qna.answer"
                                  name="answer"></textarea>
        </mat-form-field>
        <button mat-stroked-button color="warn"
                (click)="removeQnApair(i)">
            Remove this
        </button>
    </mat-card> 
</div>

在ts文件中

removeQnApair(i) {
    this.interview.qnas.splice(i,1);
     }

addQnApair(){
   this.interview.qnas.push({id: this.interview.qnas.length+1,
                question: '',
                answer: ''});
   }

-1
投票

试试这个

 <mat-card>
           <button mat-stroked-button color="primary" style="margin-right: 20px;">
               Add a new Q & A pair
            </button>
            </mat-card>
            <div *ngFor="let qna of interview.qnas">
                <mat-card id="{{qna.id}}" style="margin-bottom: 25px;">
                    <mat-form-field>
                        <input matInput type="text" class="form-control" placeholder="Interview question" required value="{{qna.question}}" name="question">
                    </mat-form-field> 
                    <mat-form-field>
                        <textarea matInput rows="5" class="form-control" placeholder="Ideal answer from the talent"  value="{{qna.answer}}" name="answer"></textarea>
                    </mat-form-field>
                    <button mat-stroked-button color="warn">
                        Remove this
                    </button>
                </mat-card> 
            </div>
© www.soinside.com 2019 - 2024. All rights reserved.