0

I am using Ionic Framework and angularjs to develop one application. Here I am using checkbox inside ng-repeat. Using this I can able to insert checkbox checked values into array. But It is inserting like strings.

Like ["coding","testing"] .

But I want it like objects.

Like ["object","object"] . Inside that object values should be there.

Html code is

<div class="action-checkbox" ng-repeat="task in projecttasks"> <h3>{{task.projectName}}</h3> <ul> <li ng-repeat="subtask in task.subTasks" ng-click="addprjtaskList(task,subtask)"> <input id="{{subtask._id}}" name="{{subtask._id}}" type="checkbox" value="{{subtask.subTaskName}}" ng-checked="selection.indexOf(subtask.subTaskName) > -1" ng-click="toggleSelection(subtask.subTaskName)" class="hide"/> <label for="{{subtask._id}}" > {{subtask.subTaskName}} </label> </li> </ul> </div> 

controller code is

$scope.selection = []; // toggle selection for a given fruit by name $scope.toggleSelection = function toggleSelection(fruitName) { var idx = $scope.selection.indexOf(fruitName); // is currently selected if (idx > -1) { $scope.selection.splice(idx, 1); console.log($scope.selection); } // is newly selected else { $scope.selection.push(fruitName); console.log($scope.selection); } }; 

can anyone help me to do this..

2
  • Looks to me like you are pushing the name, not the object. Isn't the subtask the object that you actually want?CommentedDec 23, 2015 at 17:45
  • $scope.selection.push(fruitName) will push a string value into selection array. $scope.selection.push({ fruitName: fruitName}) will push an object with one property named fruitName with the string value in variable fruitName to the array.
    – Will
    CommentedDec 23, 2015 at 17:46

2 Answers 2

4

in toggleSelection(fruitName) you are passing a string

then you are doing

$scope.selection.push(fruitName) 

it is doing what it is told!

you need to pass the object to the function ...

ng-click="toggleSelection(subtask.subTaskName)" 

SHOULD BE ...

ng-click="toggleSelection(subtask)" 

AND THEN ...

push that instead!

0
    2

    What I think you're looking for is to push subtask into an array, not subtask.subTaskName.

    You'll have to change your view to ng-click="toggleSelection(subtask)" to reference the object instead of the object's name.

    You'll also have to modify your ng-checked="selection.indexOf(subtask.subTaskName) > -1 to reference a new function that checks for duplicity (since indexOf() doesn't like object arrays) . I'll go into that further next.

    Next, you'll have to update your controller to test for duplicates. I suggest making a separate method using a for loop to iterate through your array, checking subTaskName for equality instead of indexOf(). By making a separate method, you remove logic from your html, and allow code reuse. A double win!

    Then, you'll just have to push the object normally, and you're done!

    2
    • duplicity == synonyms: deceitfulness, deceit, deception, double-dealing, underhandedness, dishonesty, fraud, fraudulence, sharp practice, chicanery, trickery, subterfuge, skulduggery, treachery ;.)
      – georgeawg
      CommentedDec 23, 2015 at 19:05
    • Thanks George, but can't we agree that object comparisons can be very deceptive? :)
      – Jon
      CommentedDec 23, 2015 at 20:34

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.