通过 $index 或 indexOf() 删除项目会导致删除 ng-repeat 中的所有项目
Deleting an item by $index or indexOf() results in deleting all items in ng-repeat
我有一个 Ionic 列表,其中的项目包括来自指令 ion-delete-button 的属性(参见 docs)。
我在以下事项中使用 ng-repeat 填充列表:
HTML
<ion-list>
<ion-item class="item item-thumbnail-left item-text-wrap"
ng-repeat="albumname in Albums track by $index"
href="#/tab/dash/{{Albums[$index]}}">
<img ng-src="{{thumbnailImage(albumname, 'album')}}" class="thumbnail-album">
<h2 style="padding-top: 20px">{{albumname}}</h2>
<!--WORKS FINE, CHANGES NAME OF $index -->
<ion-option-button class="button-energized" ng-click="changeAlbumName($index)">Change Name</ion-option-button>
<!-- DOES NOT WORK AS EXPECTED, ON PHONE DELETES ALL ALBUMS -->
<ion-delete-button class="ion-minus-circled" ng-click="deleteAlbum($index)"></ion-delete-button>
</ion-item>
</ion-list>
控制器
// Initialized and Refreshed on Updates
$scope.Albums = DashFactory.getAlbums();
$scope.deleteAlbum = function(index) {
console.log(index) // SHOWS CORRECT INDEX
var confirmPopup = $ionicPopup.confirm({
title: 'Delete Album: ' + $scope.Albums[index],
template: 'Are you sure you want to delete this album?'
});
confirmPopup.then(function(res) {
if(res) {
console.log('You are sure');
DashFactory.deleteAlbum(index); // SERVICE FOR DELETING THE ALBUM
$ionicListDelegate.showDelete(false);
} else {
console.log('You are not sure');
}
});
}
服务(DashFactory)
var Albums = ['foobut', 'barplug', 'fooin', 'barass']
var deleteAlbum = function(index) {
Albums.splice(index);
this.saveAlbums();
};
var getAlbums = function() {
return Albums;
};
解决了。基本上你必须写:
Array.splice(index, amount)
其中 amount 为 1,即您要删除的项目数。否则它将全部删除。
Array.splice 需要多个参数,它现在将删除从您提供的索引开始的所有内容。试试 Album.splice(index,1);
我有一个 Ionic 列表,其中的项目包括来自指令 ion-delete-button 的属性(参见 docs)。
我在以下事项中使用 ng-repeat 填充列表:
HTML
<ion-list>
<ion-item class="item item-thumbnail-left item-text-wrap"
ng-repeat="albumname in Albums track by $index"
href="#/tab/dash/{{Albums[$index]}}">
<img ng-src="{{thumbnailImage(albumname, 'album')}}" class="thumbnail-album">
<h2 style="padding-top: 20px">{{albumname}}</h2>
<!--WORKS FINE, CHANGES NAME OF $index -->
<ion-option-button class="button-energized" ng-click="changeAlbumName($index)">Change Name</ion-option-button>
<!-- DOES NOT WORK AS EXPECTED, ON PHONE DELETES ALL ALBUMS -->
<ion-delete-button class="ion-minus-circled" ng-click="deleteAlbum($index)"></ion-delete-button>
</ion-item>
</ion-list>
控制器
// Initialized and Refreshed on Updates
$scope.Albums = DashFactory.getAlbums();
$scope.deleteAlbum = function(index) {
console.log(index) // SHOWS CORRECT INDEX
var confirmPopup = $ionicPopup.confirm({
title: 'Delete Album: ' + $scope.Albums[index],
template: 'Are you sure you want to delete this album?'
});
confirmPopup.then(function(res) {
if(res) {
console.log('You are sure');
DashFactory.deleteAlbum(index); // SERVICE FOR DELETING THE ALBUM
$ionicListDelegate.showDelete(false);
} else {
console.log('You are not sure');
}
});
}
服务(DashFactory)
var Albums = ['foobut', 'barplug', 'fooin', 'barass']
var deleteAlbum = function(index) {
Albums.splice(index);
this.saveAlbums();
};
var getAlbums = function() {
return Albums;
};
解决了。基本上你必须写:
Array.splice(index, amount)
其中 amount 为 1,即您要删除的项目数。否则它将全部删除。
Array.splice 需要多个参数,它现在将删除从您提供的索引开始的所有内容。试试 Album.splice(index,1);