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

时间:2015-07-02 22:35:02

标签: javascript html 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
}]

1 个答案:

答案 0 :(得分:3)

您可以将密钥作为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()将完成这项工作。见上文。