条件ng-required不适用于angularjs中的textarea

时间:2018-06-08 05:39:25

标签: angularjs html5

ng-required无法使用条件。如果ng-required条件为真,则不显示错误消息。请给我解决方案

  <form name="selfApprForm" novalidate ng-submit="SubmitAppraisal()">
       <div class="form-group">
             <textarea type="text" class="form-control" name="comments" 
               placeholder="Comment..." style=" height: 80px;" ng- 
               model="appList.Comments" ng-required="(appList.Rating>3) ? true : 
               false"> 
            </textarea>
              <!-- validation -->                          
            <div ng-class="{'has-error' : (selfApprForm.comments.$dirty || 
                 selfApprForm.comments.$touched) && 
                 selfApprForm.comments.$invalid}">
                <div class="errorMsg">
                     <div ng-show="(selfApprForm.comments.$dirty || 
                                    selfApprForm.comments.$touched) ">
                          <div ng-show="selfApprForm.comments.$error.required">
                               If Rating is 4 to 5, employee or reviewer needs 
                               to justify with clomments.
                          </div>
                     </div>
               </div>
           </div>
        </div>
         <!--Button-->
         <div class="form-group">
              <button class="btn btn-primary" type="submit" value="submit" ng- 
                      disabled="selfApprForm.$invalid">{{UiLabels.btn_submit }} 
               </button>
         </div>
     </form>

1 个答案:

答案 0 :(得分:0)

您可以从

转换代码
<textarea type="text" class="form-control" name="comments" 
               placeholder="Comment..." style=" height: 80px;" ng- 
               model="appList.Comments" ng-required="(appList.Rating>3) ? true : 
               false"> 
            </textarea>

<textarea type="text" class="form-control" name="comments" 
               placeholder="Comment..." style=" height: 80px;" ng- 
               model="appList.Comments" ng-required="appList.Rating>3"> 
            </textarea>

作为角度评估表达式 appList.Rating&gt; 3 ,如同/ true。

上述解决方案应该有效,假设您的代码中没有其他错误发生。