Как сделать проверку переключателей в AgularJS - PullRequest
0 голосов
/ 26 декабря 2018

Я новичок в AngularJS, и я пытаюсь отобразить проверки ошибок начальной загрузки в AngularJS, используя приведенный ниже код. У меня возникают две проблемы

1) При использовании приведенного ниже кода сообщение об ошибке группы радиосвязи не отображается. Я действительно не понимаюпочему он не работает

2) Вторая проблема заключается в том, что номер телефона требуется только при выборе радиокнопки номера телефона. В противном случае это не требуется. Как я могу это сделать?

Может ли кто-нибудь помочь мне?пожалуйста

код

<form name="studentForm" ng-submit="saveEmployee()" role="form" novalidate>

    <div class="panel panel-primary">

        <div class="panel-heading">
            <h3 class="panel-title">Welcome</h3>
        </div>

        <div class="panel-body">

 <div class="form-group" ng-class="{'has-error': studentForm.phoneNumber.$touched && studentForm.phoneNumber.$invalid,
            'has-success': studentForm.phoneNumber.$valid }">
                <label for="phoneNumber" class="control-label">Phone Number</label>
                <input type="text" id="phoneNumber" name="phoneNumber" ng-model="employee.phoneNumber" class="form-control"
                    required>
                <span class="help-block" ng-if="studentForm.phoneNumber.$touched && studentForm.phoneNumber.$invalid">
                    Phone Number is required
                </span>
            </div>

 <div class="form-group" ng-class="{'has-error': studentForm.contactPreference.$touched && studentForm.contactPreference.$invalid,
            'has-success': studentForm.contactPreference.$valid }">
                <label for="gender" class="control-label">Contact Preference</label>
                <div class="form-control">
                    <label class="radio-inline">
                        <input type="radio" name="contactPreference" ng-model="employee.contactPreference" value="email"
                            required>
                        Email
                    </label>
                    <label class="radio-inline">
                        <input type="radio" name="contactPreference" ng-model="employee.contactPreference" value="phone"
                            required>
                        Phone Number
                    </label>
                </div>
                <span class="help-block" ng-if="studentForm.contactPreference.$touched && studentForm.contactPreference.$invalid">
                    Cintact preference is required
                </span>
            </div>
    </div>
</div>
</form>

1 Ответ

0 голосов
/ 26 декабря 2018

Просто используйте обязательный = "! Employee.contactPreference"

<div class="form-group" ng-class="{'has-error': studentForm.contactPreference.$touched && studentForm.contactPreference.$invalid,
            'has-success': studentForm.contactPreference.$valid }">
                <label for="gender" class="control-label">Contact Preference</label>
                <div class="form-control">
                    <label class="radio-inline">
                        <input type="radio" name="contactPreference" ng-model="employee.contactPreference" value="email" ng-required="!employee.contactPreference">
                        Email
                    </label>
                    <label class="radio-inline">
                        <input type="radio" name="contactPreference" ng-model="employee.contactPreference" value="phone" ng-required="!employee.contactPreference">
                        Phone Number
                    </label>
                </div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...