使用Angular从数组中删除项目

LOT*_*SMS 8 angularjs

是的,之前有人问过,我已经阅读了所有的答案,但似乎没有任何效果.所以我要求额外的一双眼睛,看看你是否能在我的代码中发现任何单一性,使其无法正常工作.(我在其他地方尝试了这个代码和逻辑,似乎工作正常).顺便说一句,控制台没有错误

当有人点击图片上的x时,我只是想从视图中删除一个项目.

这是控制器

app.controller('galleryController', ['$scope', '$http', function($scope, $http) {
    $http.get('data/galleries.json').success(function(data){
        $scope.galleries = data;
    }).error(function(error) {
        console.log(error);
    });

    $scope.removeGalleryItem=function(gallery){
        var removedGallery = $scope.galleries.indexOf(gallery);
        $scope.galleries.splice(removedGallery, 1);
    };
}]);
Run Code Online (Sandbox Code Playgroud)

和我的看法

<div class="col-xs-12 col-md-3" ng-repeat="gallery in galleries" >
    <a class="gallery-item" ng-href="{{gallery.img}}" ng-class="{true:'active',false:''}[checked]"
       title="Nature Image 1" >
        <div class="image">
            <img ng-src="{{gallery.img}}" alt="Nature Image 1"/>

        </div>
        <div class="meta">
            <strong>{{gallery.title}}</strong>
            <span>{{gallery.desc}}</span>
        </div>
    </a>
    <ul class="gallery-item-controls">
        <li><label class="check"><input type="checkbox" class="icheckbox" ng-model="checked" /></label></li>
        <li><span class="gallery-item-remove"><i class="fa fa-times" ng-click="removeGalleryItem(gallery)"></i></span></li>
    </ul>
</div>
Run Code Online (Sandbox Code Playgroud)

Angular 1.5.8

谢谢

Pun*_*jar 12

您可以$index像这样传递点击功能.

<i class="fa fa-times" ng-click="removeGalleryItem(galleryItem, $event , $index)">
Run Code Online (Sandbox Code Playgroud)

$scope.galleries.splice(index, 1);在你的点击功能中使用 removeGalleryItem,确保你有这样的索引参数.

$scope.removeGalleryItem = function(gallery , event, index){
        $scope.galleries.splice(index, 1);
    };
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助..