Angular 删除所选行索引
大家好,我在删除问题表中的行表时遇到一些问题 这是我在component.ts中的函数deleterowtableAngular 删除所选行索引,angular,Angular,大家好,我在删除问题表中的行表时遇到一些问题 这是我在component.ts中的函数deleterowtable deleteQuestions(questionsId: number) { this.questionerDetails.removeAt(1); } 这是我的组件html <!--qeustion input--> <div formArrayName="questionerDetails" class="rap"
deleteQuestions(questionsId: number) {
this.questionerDetails.removeAt(1);
}
这是我的组件html
<!--qeustion input-->
<div formArrayName="questionerDetails" class="rap">
<table id="myTable" align="center" class="table table-bordered">
<tr><th>Questions</th><th>Actions</th></tr>
<tr *ngFor="let questions of questionerDetails.controls; let i=index" [formGroupName]="i">
<td><input style="width:100%;" formControlName="questionDetail" class="form-control" placeholder="type your questions here" /></td>
<td align="center">
<button (click)="deleteQuestions()" type="button" class="btn btn-success bc"><i class="fa fa-trash"></i></button>
</td>
</tr>
</table>
</div>
问题及建议
我的问题是我只能删除行表的最后一个索引或第一个索引示例:我有5行表[0]、[2]、[3]、[4],我想删除编号删除该行,然后删除编号[2]。因此,行表有[0],[4]。
选择行时如何删除该行?
谢谢。您应该通过删除方法传递所选索引
(单击)=“删除问题(i)”
这一行给定所选索引let i=index
您的方法应该是使用splice()
而不是removeAt()
首先。您错过了
deleteQuestion()
中的索引。这就是为什么ts找不到要删除的元素的索引。您需要将其更改为deleteQuestion(i)
,我是表中元素的索引
第二个。
您应该使用的方法是
Slice()
,而不是removeAt()
。
这是它的文件。玩得开心你是否遗漏了
i
内部deleteQuestions()
?是的,没错,我在deleteQuestions中遗漏了i(i)好的,谢谢你的回答,但我使用了有效的removeAt,我只在deleteQuestions()中遗漏了i@TM你确定removeAt对你有效吗??是的,当然,在我的代码中使用removeAt是可行的。如果我使用.splice get错误“属性片在FormArray类型上不存在”。
deleteQuestions(index: number) {
this.questionerDetails.splice(index, 1);
}