简体   繁体   中英

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,
    }
  ]
}

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}}"
  checked="vm.isInState(states)">{{states.name}}

function isInState(state) {
  return vm.pager.findIndex((item) => item.location[0].state === state) > 0 ? true : false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM