是的,它之前已经被问过了,我已经阅读了所有的答案,但似乎没有任何工作。所以我要求额外的一双眼睛,看看你是否能够在我的代码中发现任何奇点,使它无法正常工作。 (我试过这个代码和逻辑的其他地方,它似乎工作正常)。在控制台中没有错误的方式使用Angular从阵列中删除项目
我只是试图从某个视图中删除一个项目,当有人点击图片上的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);
};
}]);
和我的观点
<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>
角1.5.8
感谢
你的意思是你想从DOM和Array中摧毁那个特定的对象? – AndreaM16
好问题。只是从DOM。这是为了嘲笑的目的。向客户展示当全部开发时会发生什么。那时它会被从数据库中删除,但不是现在 – LOTUSMS
检查我的答案。我使用'Lodash'作为对象和数组。它很容易摆脱这样的事情。 Lodash:https://lodash.com/docs/4.16.6 – AndreaM16