0

我正在 Angular (1.x) 中创建一个表单,并使用 ng-model 和 JSON 来预填充选中的单选框和复选框。由于某种原因,在我再次手动检查单选或复选框元素之前,预填充的值不会显示在表单范围内。如何获取表单数据以收集预填充的输入?

这是代码:

<tr ng-repeat="option in item.reportOptions" >
            <td style="width: 100%;">
    <h5><strong>{{option.title}}</strong></h5>
    <!-- Check All -->
    <div class="form-group" ng-if="option.type=='checkbox'">
    <label for="{{option.name}}" class="checkbox-inline">(check all)&nbsp;&nbsp;<strong>{{option.label}}</strong>
        <input type="checkbox" name="{{option.name}}" ng-model="checkAll" ng-value="{{option.value}}" ng-checked="option.checked==true" ng-click="selectAll()">
      </label>
    </div>
    <!-- Person -->
    <div class="form-group" ng-if="option.type=='select' && option.name=='person'">
            <select ng-model="cerOptions.primary">
                <option ng-init="cerOptions.primary=user[0]" ng-repeat="user in selectionData" ng-value="{{user.NewID}}">{{user.NewID}}--{{user.lastName}},{{user.firstName}}</option>
            </select>
    </div>
    <!-- Mask Names -->
    <div class="form-group" ng-if="option.type=='select' && option.name=='maskNames'">
            <select name="{{option.name}}" ng-model="maskNames">
                <option ng-selected="maskNames == option.value" ng-repeat="opt in option.options" ng-value="opt.value">{{opt.label}}</option>
            </select>
            <div ng-if="maskNames=='yes'">
            <p class="help-text">Check the user you would like to mask, then enter name.</p>
                <div class="form-group" ng-repeat="user in selectionData">
                <div class="col-sm-2">
                    <label class="checkbox-inline">{{user.NewID}}--{{user.lastName}},{{user.firstName}}
                        <input type="checkbox" ng-model="toggleText">
                    </label>
                </div>
                <div class="col-sm-2">
                    <input type="text" ng-if="toggleText" ng-model="cerOptions.maskName[user.NewID]" value="Person{{$index}}">
                </div>
                </div>
            </div>
    </div>
    <!-- Name Tags -->
    <div class="form-group" ng-if="option.type=='nameTag'">
        <table class="table table-striped table-condensed" style="width: 50%;">
            <thead>
                <tr>
                    <th>New ID</th>
                    <th>Last Name</th>
                    <th>First Name or Nickname</th>
                    <th>Company</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="user in selectionData">  
                    <td>{{user.NewID}}</td>
                    <td><input type="text" ng-model="cerOptions.nameTagsLast[user.NewID]" ng-init="cerOptions.nameTagsLast[user.NewID]=[user.lastName]" value="{{user.lastName}}"></td>
                    <td><input type="text" ng-model="cerOptions.nameTagsFirst[user.NewID]" ng-init="cerOptions.nameTagsFirst[user.NewID]=[user.firstName]" value="{{user.firstName}}"></td>
                    <td><input type="text" ng-model="cerOptions.nameTagsComp[user.NewID]" ng-init="cerOptions.nameTagsComp[user.NewID]=[user.accountName]" value="{{user.accountName}}"></td>
                </tr>
            </tbody>
         </table>
    </div>
    <!-- Nested Checkboxes and Radios -->
    <div class="form-group" ng-if="option.options.length > 0">
      <label ng-repeat="deet in option.options" ng-if="deet.type=='radio'" for="{{deet.name}}" class="radio-inline">{{deet.label}}
        <input type="radio" name="{{deet.name}}" ng-model="cerOptions.options[deet.name]" ng-value="{{deet.value}}">
      </label>
      <label ng-repeat="deet in option.options" ng-if="deet.type=='checkbox'" for="{{deet.name}}" class="checkbox-inline">{{deet.label}}
        <input type="checkbox" name="{{deet.name}}" ng-model="cerOptions.options[deet.name]" ng-value="{{deet.value}}" ng-checked="{{deet.checked}}">
      </label>
    </div>
4

1 回答 1

0

事实证明,我只需要在输入元素上使用 ng-init 初始化模型值。

于 2016-08-04T19:29:19.287 回答