0

I have a select dropdown. Upon selecting each value in dropdown, checkboxes are created using ng-repeat. All the values in the dropdown are supposed to be selected and corresponding checkboxes must be checked and stored in an array.

Whenever a checkbox changes I add it or remove it from array. When I change the dropdown, the checkboxes checked for previous dropdowns are gone.

How do I keep them checked based on the array that stores the checked values.

<div ng-repeat="flat in flatsArray" class="row" style="width:90%;">
       <div class="col-md-2">
          <input 
               type="checkbox" ng-true-value="{{flat.flat_id}}"
               ng-false-value="'-1'" 
               //tried this but selctedFlats is an object
               ng-checked="userEventData.selectedFlats.indexOf(sFlats[$index])"  `userEventData.selectedFlats` is an object
              ng-change="checkChanged(sFlats[$index], flat)" 
              ng-model="sFlats[$index]" />
       </div>
       <div class="col-md-10"><label for="{{flat.flat_no}}">{{flat.flat_no}}</label></div>
    </div>

$scope.userEventData.selectedFlats is the array of checked values.

I could have done ng-checked="userEventData.selectedFlats.indexOf(sFlats[$index])" but selectedFlats contains object. I want to check with a value in the object

  • what is the definition of selected Flats? Is it an object, and not an array? – Fallenreaper Jun 18 '18 at 13:47
  • @Fallenreaper it's an array of objects. –  Jun 18 '18 at 13:51
  • Well indexOf returns an INDEX, int, >= -1, So you would want to maybe change your expression slightly? It doesnt want an object, it wants a boolean. I feel this is harder than it should be. Why not define a checked property within the flatsArray Object and just say: `ng-checked="flat.checked"` It would create less Iteration, and cleaner code – Fallenreaper Jun 18 '18 at 13:55
  • @Fallenreaper hmm, ok. Write as answer and I will accept –  Jun 18 '18 at 14:06
  • 1
    Why are you using `ng-checked` with `ng-model` when the documentation tells you not to do that? – georgeawg Jun 18 '18 at 15:35

1 Answers1

0

NG-CHECKED is looking for an expression returning a state. indexOf will return a numeric value >= -1. Though this can be done will create a lot more processing because it will be running the functions over and over again to look for changes.

I would recommend something akin to: when you get your flats data, give the checked property there and assign it to what it needs to be. It would create cleaner code, and you have 1 object to manipulate. You can always parse it as needed.

ng-checked="flat.checked"

looks far cleaner, and creates less processing through looping arrays CONTINUALLY looking for changes.

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
  • hey i am trying what you said.. when I change the dropdown values, for some reason the `ng-change` on some selected checkboxes are not called. Can you think of a reason? –  Jun 19 '18 at 05:17
  • here's the question https://stackoverflow.com/questions/50921448/ng-change-not-getting-called-on-checkbox –  Jun 19 '18 at 09:48