简体   繁体   English

Angular在ng-repeat(MEAN-Stack)中传递错误的对象

[英]Angular passes wrong object in ng-repeat (MEAN-Stack)

I am building a platform to manage exams for my institute. 我正在建立一个平台来管理我的学院的考试。 I use the MEAN-Stack. 我使用MEAN-Stack。

Everything works fine except for deleting a "klausur" (german for exam). 一切正常,除了删除“ klausur”(德语考试)。 The Code is below. 代码如下。 If i click the last link (using the "deleteKlausur(klausur)" click) on any object of the table, not the momentary object, but the last of my one is deleted inside of my table. 如果我在表的任何对象上单击最后一个链接(使用“ deleteKlausur(klausur)”单击),则不是瞬时对象,而是我表中的最后一个链接被删除。 Inside of the DB, the right one is deleted. 在数据库内部,右边的一个被删除。 If i then click on the same button again, the server breaks down and because it tries to delete the same ID again, giving me a problem concerning a null object. 如果我然后再次单击相同的按钮,服务器将崩溃,因为它试图再次删除相同的ID,这给我带来了一个有关空对象的问题。

<table class="table table-hover table-striped">
            <thead>
            <th>ID</th>
            <th>Name</th>
            <th>Datum</th>
            <th>Semester</th>
            <th>Aufgabenzahl</th>
            <th>Teilnehmer</th>
            <th>Aktionen</th>
            </thead>
            <tbody>
            <tr ng-repeat="klausur in klausuren">
                <td>{{klausur._id}}</td>
                <td>{{klausur.name}}</td>
                <td>{{klausur.gehaltenAm | date:'dd.MM.yy'}}
                    <br/>{{klausur.gehaltenAm | date:'H:mm'}}
                </td>
                <td>{{klausur.semester}}</td>
                <td>{{klausur.aufgaben.length}}</td>
                <td>{{klausur.teilnehmer.length}}</td>
                <td><a href="#/klausuren/{{klausur._id}}/edit" class="btn btn-default" style="width:100%">Klausur
                    ändern</a><br/>
                    <a href="" ng-click="deleteKlausur(klausur)" class="btn btn-danger" style="width:100%">Klausur löschen</a></td>
            </tr>
            </tbody>

My JS (using Angular) script is as following: 我的JS(使用Angular)脚本如下:

app.controller('KlausurListController', function ($scope, $http) {
$http.get('http://localhost:3000/klausuren').success(function (response) {
    $scope.klausuren = response;
}).error(function (err) {
    $scope.error = err;
});

$scope.deleteKlausur = function (klausur) {
    $http.delete('http://localhost:3000/klausuren/'+ klausur._id).success(function(res){
        $scope.klausuren.pop(klausur);
    });
}});

Thank you , even for reading the whole thing! 谢谢,甚至阅读了整本书! Hope you can help! 希望能对您有所帮助!

You are using pop() which only removes the last element in an array. 您使用的pop()仅删除数组中的最后一个元素。

To remove the correct one you need to find it's index in array and use splice() 要删除正确的索引,您需要在数组中找到它的索引并使用splice()

$scope.deleteKlausur = function (klausur) {
    $http.delete('http://localhost:3000/klausuren/'+ klausur._id).success(function(res){
        var index = $scope.klausuren.indexOf(klausur);
        if(index !== -1){
           $scope.klausuren.splice(index,1);
        }            
    });
}});

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

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