繁体   English   中英

如何使用AngularJS从对象列表中获取ID?

[英]How to get id from list of object using AngularJS?

我在$ on上有api调用,我得到了附带问题的Json响应。 我能够在li显示fileName,现在我具有删除功能,当用户单击删除图标时,我正在调用函数并尝试获取rskAsesAprvAtchKy密钥,因此我可以将密钥发布到后端以删除此文件。

这是不确定的,我不确定我缺少什么帮助,我们将不胜感激。

main.html

<div class="row">
    <div class="col-md-6">
        <ul>
            <li ng - repeat="file in attachedDoc">{{file . fileName}}
                <a href="" ng - click="deleteFile()">
                    <span class="glyph_remove"></span>
                </a>
            </li>
        </ul>
    </div>
</div>

factory.js

$scope.$on('addEditAttest', function (s, attestorObj) {
    $scope.attestorObj = attestorObj;
    attestorFactory.getAttachedDocument($scope.attestorObj.riskAssessmentRoleAsgnKey)
        .then(function (response) {
            $scope.attachedDoc = response.data;
        });
});

$scope.deleteFile = function () {
    var fileKey;
    $scope.attachedDoc.rskAsesAprvAtchKy = fileKey;
    console.log("deleted", fileKey);
}

JSON.JS

[{
    "rskAsesAprvAtchKy": 1001,
    "fileName": "Doc 1",
    "rskAsesRoleAsgnKy": 1277
}]

您可以将键作为ng-click方法的参数传递:

在视图

<li ng-repeat="file in attachedDoc">{{file.fileName}}
     <a href="" ng-click="deleteFile(file.rskAsesAprvAtchKy, $index)"> //Key from the file
        <span class="glyph_remove">
        </span>
     </a>
</li>

更改删除方法

$scope.deleteFile = function(fileKey, fileIndex){
   /*Delete the file*/
   $scope.attachedDoc.splice(fileIndex, 1); //remove the file at position fileIndex 
}

编辑:

从ng-repeat传递$ index并使用Array.splice()将完成此工作。 往上看。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM