[英]I can´t remove the last item of array
I can remove any item of array unless the last one.我可以删除数组的任何项目,除非最后一个。 I also use angularjs to show information in the view.
我还使用 angularjs 在视图中显示信息。 I don´t know what is happening with the last item of this array.
我不知道这个数组的最后一项发生了什么。 Please, anyone can help me?
拜托,有人可以帮我吗?
Here is HTML:这是 HTML:
<div class="row">
<div class="col-md-12">
<h4><strong>Documento {{index + 1}} de {{documentos.length}}:</strong> {{documentos[index].nome}}</h4>
<iframe style="background: #ccc;" ng-show="exibirPreview" frameborder="0" ng-src="{{versaoLink}}" width="100%" height="300px"></iframe>
<div class="alert alert-warning" ng-hide="exibirPreview">
@Html.Raw(Resources.Dialog.SignDocuments.TypeDocumentCanNotPreDisplayed)
</div>
<hr />
<div class="pull-right btn-row" ng-show="documentos.length > 1">
<button class="btn btn-default" type="button" ng-click="RemoveDoc(index)"><i class="fa fa-fw fa-times"></i> @Resources.Common.RemoveDocList</button>
</div>
</div>
</div>
Here is js/angularjs这是 js/angularjs
$scope.documentos = [
{nome:"document1", chave: "8F65579E3737706F", extensao:".pdf"},
{nome:"document2", chave: "8F65579E3730007F", extensao:".pdf"},
{nome:"document3", chave: "8545579E3737706F", extensao:".pdf"},
{nome:"document4", chave: "8555579E3730007F", extensao:".pdf"}
]
$scope.ViewLink = function () {
var versao = $scope.documentos[$scope.index];
$scope.exibirPreview = versao.extensao == ".pdf" || versao.extensao == ".txt";
if (!$scope.exibirPreview) {
$scope.versaoLink = '';
} else {
$scope.versaoLink = '/Documento/VersaoView?chave=' + versao.chave;
}
};
$scope.ViewLink();
$scope.RemoveDoc = function (index) {
$scope.documentos.splice(index, 1);
$scope.ViewLink();
};
In your HTML you are preventing the deletion of the last element:在您的 HTML 中,您正在阻止删除最后一个元素:
<div class="pull-right btn-row" ng-show="documentos.length > 1">
<!-- -->
</div>
documentos.length > 1
means "hide when it reaches one item in the array". documentos.length > 1
表示“当它到达数组中的一项时隐藏”。
It should be documentos.length == 0
.它应该是
documentos.length == 0
。
It's either this or your index
value starts from 1 and not from 0.要么是这个,要么是你的
index
值从 1 开始,而不是从 0 开始。
The simplest solution would be to change your remove function to take in the document instead of the index.最简单的解决方案是更改删除函数以接收文档而不是索引。 Try this:
尝试这个:
$scope.RemoveDoc = function(document) {
var index = $scope.documents.indexOf(document);
$scope.documents.splice(index, 1);
}
in view:鉴于:
<button class="btn" type="button" ng-click="RemoveDoc(document)">Delete</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.