Why does my entire list get destroyed when trying to remove an item using splice in JavaScript?
HTML:
<ul class="nav nav-tabs">
<li ng-repeat="pane in panes" ng-class="{active:pane.selected}">
<a href="" ng-click="select(pane)">
{{pane.title}}
<sup ng-click="close(pane)">x</sup>
</a>
</li>
<li>
<a href="" ng-click="createPane()">+</a>
</li>
</ul>
JS
.controller('editCtrl', ['$scope', function($scope){
$scope.panes = [];
var ctrl = this;
$scope.select = function(pane) {
angular.forEach($scope.panes, function(pane) {
pane.selected = false;
});
pane.selected = true;
};
this.addPane = function(pane) {
if ($scope.panes.length === 0) {
$scope.select(pane);
}
$scope.panes.push(pane);
};
$scope.createPane = function() {
var pane = {
title: 'untitled',
content: 'Scope:'+$scope.$id
}
ctrl.addPane(pane);
};
$scope.close = function(pane) {
var idx = $scope.panes.indexOf(pane);
if (idx != -1) $scope.panes.splice(idx, 1);
}
}]);
I am specifically focused on the close method and looking for a solution.