角度复选框ng将模型作为数组

Angular checkbox ng-model as an array

本文关键字:数组 模型 复选框 ng      更新时间:2023-09-26

我的HTML是

    <label>
  <input type="checkbox" name="Communities" ng-model="formData.User.Communities" value="1" id="YOURROLE_0">
  COACH</label>
<br>
<label>
  <input type="checkbox" name="Communities" ng-model="formData.User.Communities" value="2" id="YOURROLE_1">
  ATHLETE</label>
<br>
<label>
  <input type="checkbox" name="Communities" ng-model="formData.User.Communities" value="3" id="YOURROLE_2">
  PARENT</label>

我如何指定HTML/Javascript,我得到的模型如下。我需要复选框(选中的)作为数组填充到数组中。

.controller('formController', function($scope) {
        $scope.formData = {
            "User": {
                "Communities": [1, 2, 3]
            }
        };
    });

清单模型是的答案

http://vitalets.github.io/checklist-model/