2

The array is structured like so

  $scope.structure = {
    sections: [{
      id:0,
      sectionItems: []
    },{
      id:1,
      sectionItems: []
    },{
      id:2,
      sectionItems: []
    }]
};

I have a nested ng-repeat so I can show items within sectionItems[] (Inside should be objects and one of the keys is Name - not relevant)

<div ng-repeat="sections in structure.sections" class="col-md-12">
   <div class="panel panel-info">
    <ul class="screenW-section">
        <li class="col-xs-6" ng-repeat="item in sections.sectionItems" 
         ng-click="item.splice($index, 1)">
            {{item.Name}}
        </li>
    </ul>
</div> </div>

I want to be able to remove items on click but the ng-click="item.splice($index, 1) Is not working no matter how I format it.

1
  • All of your stuff works, problem was in another part of a code Commented Apr 26, 2016 at 15:30

3 Answers 3

3

try this:

var app = angular.module("testApp", []);
app.controller('testCtrl', function($scope){
  $scope.structure = {
    sections: [{
      id:0,
      sectionItems: ['1','2','3']
    },{
      id:1,
      sectionItems: ['11','21','32']
    },{
      id:2,
      sectionItems: ['12','22','32']
    }]
};
  
  
  $scope.remove = function(sectionItems,index){
     sectionItems.splice(index, 1);     
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="testApp" ng-controller="testCtrl">

  <div ng-repeat="sections in structure.sections" class="col-md-12">
   <div class="panel panel-info">
    <ul class="screenW-section">
        <li class="col-xs-6" ng-repeat="item in sections.sectionItems" 
         ng-click="remove(sections.sectionItems,$index)">
            {{item}}
        </li>
    </ul>
</div> </div>

</div>

Sign up to request clarification or add additional context in comments.

Comments

3

To remove an item you need to remove it from the array.
So, for example, you could do

ng-click="remove(sections.sectionItems, $index)"

in the view, and

$scope.remove = function(array, index) { 
  array.splice(index, 1);     
}

in the controller...

4 Comments

"sections" and "sectionItems" are arrays and cannot be referenced that way.
Since it's nested you would need the index of both the section and the sectionItem. In other words, you need a reference to the correct section also in your answer.
@CountGradsky: you are using it as objects in the view you show... Am I wrong?
@MarcoS maybe you ment $scope.structure.sections[n].sectionItems.splice(index, 1);
1

You're calling splice on the item and not the array

ng-click="sections.sectionItems.splice($index, 1)"

1 Comment

I am getting "TypeError: scope.$eval(...).splice is not a function" on every solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.