John Baxter John Baxter - 3 years ago 113
Javascript Question

AngularJS 1.5 need to have checkbox set to be checked with model of objects without ng-checked

Because of some other conflicts and dependencies , i'm not using angularjs 1.6.x , i'm using 1.5.x so i'm not sure if there is some bug - but I just know ng-checked is just awful

So I have

ng-repeat


2 objects


  1. vm.stateList contains all 50 states

  2. vm.pager contains json like object in which if a state WAS selected it WILL be in the list



Thus I'm looping through displaying all the states as checkboxes, but I WANT to set them to be CHECKED - if they are found in my vm.pager object

div ng-repeat="states in vm.statesList"


checkbox I want checked if in my object

<input ng-model="vm.pager.location[$index].state" type="checkbox" id="state{{states}}">{{states.name}}


Example of data

Notice that the
state
is found in the
location


{
"Id": 105,
"Name": "blah",
"Description": "other data",
"$$hashKey": "object:98",
"location": [
{
"Id": 96,
"state": "NY",
"Order": 9,
}
]
}

rrd rrd
Answer Source

You could make a function in your controller that is called from each checkbox:

<input ng-model="vm.pager.location[$index].state"
  type="checkbox" id="state{{states}}"
  checkbox="vm.isInState(states)">{{states.name}}

function isInState(state) {
  return vm.pager.findIndex((item) => item.location[0].state === state) > 0 ? true : false;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download