0

https://jsfiddle.net/ytygc19t/2/

HTML:

<div ng-app="app" ng-controller="MainCtrl">
  <span>Test App @ </span> status:{{loaded}}
<hr>

  <div ng-repeat="checkbox in checkBoxArray track by $index">{{checkbox}}
    <input name="x{{checkbox}}"  id="x{{checkbox}}" ng-click="removeCheckBox(checkbox)" type="checkbox"/>
  </div>
</div>

JS:

angular.module("app",[])
.controller("MainCtrl", ["$scope", "$timeout", function($scope, $timeout){
   $scope.checkBoxArray = ["a", "b", "c", "d", "e"]
   $scope.loaded = "loaded";

   $scope.removeCheckBox = function (value){
      $timeout(function(){
         $scope.checkBoxArray.splice($scope.checkBoxArray.indexOf(value), 1);
      }, 300)       
   }
}])

When i check checkbox a, a is removed, and b is also checked

1
  • the $timeout was just used to see if it was a timing issue Commented Jul 15, 2016 at 14:54

2 Answers 2

2

Remove track by $index because everytime a is selected, then a is removed and that time b index will become 1 so it is getting checked.

Find the working fiddle here : https://jsfiddle.net/varit05/ytygc19t/3/

Hope it helps you!

Cheers!

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

1 Comment

Glad it helps you. Please mark it correct if it solves your question :)
0

I think what you're experiencing is a clash between HTML checkbox functionality and Angular functionality. Try tracking the checkbox value using angular.

<li ng-repeat="item in Items">
  <label>{{item.Name}}
    <input type="checkbox" ng-model="item.Selected" ng-click="item.Selected = true" />
  </label>
</li>

And in the controller

$scope.Items = [{Name: 'bla', Selected: false}, ...]

Also, is there any particular reason you need track by $index? That could be giving you some problems as well.

Comments

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.