admin管理员组文章数量:1326332
In angular2, my HTML is calling removeThisForm in javascript. The event is an object of File Array. For each object in File Array, I generate a form in angular2.
(click)=removeThisForm(event)
In javascript, I am trying to remove the file that is passing in.
removeThisForm(file) {
var removableIndex = arr.indexOf(file);
if (removeIndex >= 0) {
arr = arr.splice(removableIndex);
}
I am able to remove any form passing in, except the first one. I tried shift(), slice() and splice(0,1). When I did splice(0,1), I am getting an error of "Form submission canceled because the form is not connected".
In angular2, my HTML is calling removeThisForm in javascript. The event is an object of File Array. For each object in File Array, I generate a form in angular2.
(click)=removeThisForm(event)
In javascript, I am trying to remove the file that is passing in.
removeThisForm(file) {
var removableIndex = arr.indexOf(file);
if (removeIndex >= 0) {
arr = arr.splice(removableIndex);
}
I am able to remove any form passing in, except the first one. I tried shift(), slice() and splice(0,1). When I did splice(0,1), I am getting an error of "Form submission canceled because the form is not connected".
Share Improve this question asked Mar 21, 2017 at 15:55 Matt-powMatt-pow 9864 gold badges18 silver badges34 bronze badges 1-
Try
arr.splice(removableIndex,1);
– Karthik VU Commented Mar 21, 2017 at 15:57
1 Answer
Reset to default 6You've omitted to pass second argument to the Array.prototype.splice method (an integer representing the number of elements to be deleted). Try this:
removeThisForm(file) {
var removableIndex = arr.indexOf(file);
if (removeIndex >= 0) {
arr.splice(removableIndex, 1);
}
}
Also, the Array.prototype.splice
method returns an array containing the deleted elements. Therefore, you can't say:
arr = arr.splice(removableIndex, 1);
as it will override your arr
with the returned value of Array.prototype.splice
method.
本文标签: angularsplice can39t remove the first element in javascriptStack Overflow
版权声明:本文标题:angular - splice can't remove the first element in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742201360a2432021.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论