AngularJS 1.5需要将复选框设置为使用没有ng-checked的对象模型进行检查

时间:2017-07-25 07:37:05

标签: javascript angularjs checkbox angularjs-ng-repeat angular-ngmodel

由于其他一些冲突和依赖,我不使用angularjs 1.6.x,我使用的是1.5.x所以我不确定是否有一些错误 - 但我只知道ng-checked非常糟糕

所以我有ng-repeat

2个对象

  1. vm.stateList包含所有50个州
  2. vm.pager包含json like对象,如果状态WAS选中它将在列表中
  3. 因此我循环显示所有状态作为复选框,但我想将它们设置为CHECKED - 如果它们在我的vm.pager对象中找到

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

    复选框我想在我的对象中检查

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

    数据示例

    请注意state

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

1 个答案:

答案 0 :(得分:0)

您可以在控制器中创建一个从每个复选框调用的函数:

<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;
}