将所需属性动态添加到angularjs

时间:2018-09-15 08:13:38

标签: angularjs validation required

我无法动态设置输入字段的必填属性 我表单的必填属性必须根据单选按钮字段动态更改。

这是将单选按钮设置为要加入screenshot时表单的外观。并且当单选按钮设置为成员/校友时,表单看起来像screenshot

我无法动态切换相应格式的输入字段的必需属性。

如果选中了新成员,这是联系人和DOB字段的代码

    <label class="control-label is-imp"></label>
                                <div class="input-group">
                                    <input type="radio" ng-model="joinFormData.joinType" value="wantsToJoin">I Want to join as member
                                    <input type="radio" ng-model="joinFormData.joinType" value="alreadyMember">I am a member/alumnus
                                </div>
                            </div>

                            <div class="form-group no-pad-left" ng-if="joinFormData.joinType == 'wantsToJoin'">
                                <div class="nss-only" ng-if="clubType == 'NSS'">
                                    <div class="form-group col-md-12 no-pad-left no-pad-right">
                                        <div class="col-md-6 no-pad-left">
                                            <label class="control-label is-imp">Contact:</label> 
                                            <input
                                                type="text" class="form-control" id="body_contact"
                                                name="body_contact" ng-model="joinFormData.contact" placeholder="1234567890" ng-pattern="/^[0-9]{10}$/" ng-minlength="10" ng-maxlength="10" integer [ng-required]="joinFormData.joinType == 'wantsToJoin'">
                                            <span ng-show="joinForm.body_contact.$error.$invalid || joinForm.body_contact.$error.pattern"><span class = "red-color">Enter a valid number!</span></span>
                                        </div>

                                        <div class="col-md-6 no-pad-right">
                                            <label class="control-label is-imp">DOB:</label> 
                                            <input
                                                type="date" class="form-control" id="body_dob"
                                                name="body_dob" ng-model="joinFormData.dob"
                                                required ng-maxlength="100" required>
                                            <span ng-show="joinForm.body_dob.$error.required && joinForm.body_dob.$touched"><span class="red-color">Required!</span></span>
                                        </div>
                                    </div> </div> </div>

如果选中“会员/校友”,这是用于选择类型字段的代码

<div class="form-group col-md-12 no-pad-left no-pad-right">
                                <div class="col-md-6 no-pad-left" ng-if="joinFormData.joinType == 'alreadyMember'">
                                    <label class="control-label is-imp">I am already a member/alumnus:</label>
                                    <div class="input-group">
                                        <select ng-model="joinFormData.memberType"
                                            class="form-control selectpicker" [required]="joinFormData.joinType == 'alreadyMember'" name="body_type">
                                            <option value="" selected disabled>Select Type</option>
                                            <option value="member">Member</option>
                                            <option value="alumni">Alumni</option>
                                        </select>
                                        <!-- <span ng-if="!joinForm.body_type.$valid && joinForm.body_type.$touched"><span class="red-color">Required!</span></span> -->
                                    </div>
                                </div></div>

我尝试使用[required] =“ joinFormData.joinType =='alreadyMember'”条件,但这没用。

请帮助我。我无法确定错误,因为我是尖锐的

0 个答案:

没有答案