2

I have a simple and strange question. I have a drop box list that i made with ng-repeat. (Below code)

 <ul>
 <li ng-repeat="item in formLIST.ContractType">
    <input  type="checkbox" ng-click="checkItems(item)" value={{item.ls_ItemIndex}} >{{item.ls_ItemValue}}
 </li>
 </ul>

And this is my Angularjs code (Below)

$scope.listitem=[];
$scope.checkItems = function (item) {
    $scope.listitem.push({
        item: item.ls_ItemValue      
    });

And my question is this: I want to know, when i check OR uncheck these beautiful small boxes, it adds or remove the value of it in my JSON file.

And now when i click on my check boxes, it will be add in my JSON. But if i click on it again for unchecked it, it will be add again in my JSON file. My code can add them several times. except remove it. And i want to add these beautifuls just once.

Pooria.Shariatzadeh
  • 291
  • 2
  • 4
  • 16

2 Answers2

1

Here:

$scope.checkItems = function (item) {
    var idx = $scope.listitem.indexOf(item.ls_ItemIndex);

    if(idx == -1) // not selected
       $scope.listitem.push(item.ls_ItemIndex);
    else // selected
       $scope.listitem.splice(idx,1);
}
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
0

I recommend that you use ng-model:

<ul>
 <li ng-repeat="item in formLIST.ContractType">
    <input  type="checkbox" ng-model="item.Is_ItemValue" />{{item.ls_ItemValue}}
 </li>
 </ul>

  <!-- selected list -->
  <ol>
  <li ng-repeat="item in formLIST.ContractType | filter: {Is_ItemValue:true}"> {{ item }}</li>
  </ol>
Michael Kang
  • 52,003
  • 16
  • 103
  • 135