admin管理员组文章数量:1336304
I am working on a project that relies heavily on Angular for its front-end. I am still in the process of learning Angular, and have e across a plex scenario.
Here is what I have:
The above is part of a wizard. Clicking Next
moves the user along the wizard. So far so good. I do not want the user to be able to click Next
unless they select an option from the drop down.
If they select the Payment Taken
option, I would like them to be able to move along the wizard, but instead what is currently happening is this:
As you can see, the Next
button is disabled when I want it to be enabled.
Moving on .. if Payment Deferred
is selected, I would like the user to input a reason for why the payment was deferred .. also this reason must be over 10 characters long, if that rule is satisfied, then the Next
button should be enabled.
The above seems to work fine!
CODE:
<form name="paymentForm">
<div class="form-group" id="Div2">
<select ng-model="selectedPaymentStatus" ng-options="ps.name for ps in paymentStatus" required></select>
</div><!-- .form_group -->
<div ng-show="selectedPaymentStatus.value === 'paymentDeferred'" class="form-group" id="Div3">
<p>Reason</p>
<textarea class="form-control" ng-disabled="selectedPaymentStatus.value === 'paymentTaken'" required ng-model="crate.paymentDeferredReason" ng-minlength="10"></textarea>
</div><!-- .form_group -->
</form>
<button class="btn wizard-next-btn pull-right" ng-disabled="!paymentForm.$valid" ng-click="nextStep()">Next</button>
<button class="btn wizard-back-btn" ng-click="previousStep()">Back</button>
WHAT I THINK:
I think the reason it is not behaving as expected when I select Payment Taken
in the drop down is because I have told my markup that the hidden Reason
textarea is also required.
To get over this, I made my Reason
text area disabled when the user selects Payment Deferred
.
ng-disabled="selectedPaymentStatus.value === 'paymentTaken'"
But this apparently does not override the validation rules set on that DOM element (the textarea); that, or I'm doing something wrong.
I am working on a project that relies heavily on Angular for its front-end. I am still in the process of learning Angular, and have e across a plex scenario.
Here is what I have:
The above is part of a wizard. Clicking Next
moves the user along the wizard. So far so good. I do not want the user to be able to click Next
unless they select an option from the drop down.
If they select the Payment Taken
option, I would like them to be able to move along the wizard, but instead what is currently happening is this:
As you can see, the Next
button is disabled when I want it to be enabled.
Moving on .. if Payment Deferred
is selected, I would like the user to input a reason for why the payment was deferred .. also this reason must be over 10 characters long, if that rule is satisfied, then the Next
button should be enabled.
The above seems to work fine!
CODE:
<form name="paymentForm">
<div class="form-group" id="Div2">
<select ng-model="selectedPaymentStatus" ng-options="ps.name for ps in paymentStatus" required></select>
</div><!-- .form_group -->
<div ng-show="selectedPaymentStatus.value === 'paymentDeferred'" class="form-group" id="Div3">
<p>Reason</p>
<textarea class="form-control" ng-disabled="selectedPaymentStatus.value === 'paymentTaken'" required ng-model="crate.paymentDeferredReason" ng-minlength="10"></textarea>
</div><!-- .form_group -->
</form>
<button class="btn wizard-next-btn pull-right" ng-disabled="!paymentForm.$valid" ng-click="nextStep()">Next</button>
<button class="btn wizard-back-btn" ng-click="previousStep()">Back</button>
WHAT I THINK:
I think the reason it is not behaving as expected when I select Payment Taken
in the drop down is because I have told my markup that the hidden Reason
textarea is also required.
To get over this, I made my Reason
text area disabled when the user selects Payment Deferred
.
ng-disabled="selectedPaymentStatus.value === 'paymentTaken'"
But this apparently does not override the validation rules set on that DOM element (the textarea); that, or I'm doing something wrong.
Share Improve this question edited Aug 4, 2018 at 20:48 halfer 20.3k19 gold badges109 silver badges202 bronze badges asked Jun 24, 2014 at 7:56 J86J86 15.3k50 gold badges145 silver badges249 bronze badges 1-
1
As a quick idea, can you replace
ng-show
withng-if
(Angular >= 1.2)?ng-if
removes the element and, hopefully, may also retract the validation failure. – Nikos Paraskevopoulos Commented Jun 24, 2014 at 8:59
2 Answers
Reset to default 5ng-show
applies the display: none
CSS rule (ref), but the functionality of the content is still present. In your case, the <textarea>
remains required, despite the block being hidden.
ng-if
on the other hand pletely removes its content from the DOM and Angular's hierarchy. The removed ng-model
controller will not take part in validation, which is what you want:
<div ng-if="selectedPaymentStatus.value === 'paymentDeferred'" ...>
<textarea ...>
</div>
This may help you
<ng-container *ngIf="selectedPaymentStatus.value === 'paymentDeferred;">
<div>
<textarea> Some Text Goes Here </textarea>
</div>
</ng-container>
本文标签: javascriptAngular Validation based on selected option in DropDownStack Overflow
版权声明:本文标题:javascript - Angular Validation based on selected option in DropDown - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742390604a2465912.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论