简体   繁体   中英

Setting radio button in ng-repeat loop to $dirty when checked

The issue I have is as follows:

I have X number of radio buttons in an ng-repeat. None of these inputs are checked on first load (View 1). The user then checks one of these inputs then moves to the next view (View 2). When I go back to the previous view (View 1). The input is checked using ng-checked="{{dealer.selected}}" .

The problem is that even though the field is 'checked' the validation class is still ng-pristine . How can I change it to dirty if pre checked (programmatically )?

Code:

                        <td>
                            <div class="selectedDealerInput">
                                <input ng-model="dealerCodeVal.name" id="dealerCode-{{dealer.dealerCode}}" name="dealerCode" value="{{dealer.dealerCode}}" ng-checked="{{dealer.selected}}" type="radio" ng-required="!dealerCodeVal.name">
                                <label for="dealerCode-{{dealer.dealerCode}}">{{dealer.dealerProfile.dealerName}} : {{dealer.selected}}</label>
                                <span data-ng-show="(dealerSelectForm.dealerCode.$dirty || dealerSelectForm.$submitted) && dealerSelectForm.dealerCode.$error.required" class="error">Please select at least one option</span>
                            </div> </td>

                    </tr>

从您的控制器:

$scope.form.dealerCode.$dirty = true;

In the controller use the $setDirty function:

$scope.form.dealerCode.$setDirty();

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