AngularJS 动态 ng 模式无效

AngularJS Dynamic ng-pattern not working

我在通过 ng-patter 验证表单时遇到问题。 ng-pattern 验证取决于另一个下拉菜单。在下拉菜单中,我有两个值 A 和 B。对于每个值,我有不同的模式来验证。现在,我面临的问题是我能够根据值调用模式,但即使我根据模式输入了正确的信息,它仍然显示无效。有关更多详细信息,请找到以下代码以获取更多详细信息。

HTML 表格

$scope.vendors = [
                            {
                                "name":"A"
                            },
                            {
                                "name":"B"
                            }
                        ]; 
                        
$scope.setVendorName = function(){
                            //alert($scope.vendorName.name);
                            localStorageService.set("vendorName",$scope.vendorName.name);
                            $scope.vendor = localStorageService.get("vendorName");
                            $scope.seatPattern = function(audi){
                                if(audi.name == 'A'){
                                    return "/^[a-fA-F]+[1-6]{1}/";
                                }else{
                                    return '/^[a-cA-C]+[1-8]{1}/';
                                }
                            }
                        }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form class="form" name="userForm" novalidate>
    <ul>
      <!--  <li ng-hide="checkRecentOrder()==false">
            <a class="btn btn-primary" href="#">Tract Your Existing Order</a>
        </li>-->
        <li> 
          <select name="vendor" ng-model="vendorName" ng-options="vendor.name for vendor in vendors" class="name" required ng-change="setVendorName()">
              <option value="">Select Audi</option>
          </select>
          <span class="error" ng-show="userForm.vendor.$error.required && userForm.vendor.$dirty" style="color:#db3721; font-weight:normal;">
              <br />Please select one option
          </span>
      </li>
      <li> 
          <input name="seat" 
                 class="name" 
                 placeholder="Seat Number "  
                 ng-model="seat_number" 
                 ng-maxlength="2" 
                 ng-minlength="2"
                 ng-pattern="seatPattern(vendorName)"
                 required/>
          <!--ng-pattern="/^[a-zA-Z]+[0-9]{1}/"-->
          <span class="error" ng-show="userForm.seat.$dirty && userForm.seat.$invalid" style="color:#db3721; font-weight:normal;">
              
              <span ng-show="userForm.seat.$error.required">
                  <br />Please enter your seat number
              </span>
              <span ng-show="userForm.seat.$error.maxlength || userForm.seat.$error.minlength || userForm.seat.$error.pattern">
                  <br />
                  Please enter seat num alpha numeric (A1)              </span>
              
          </span>
      </li>
      
        
    </ul>  
  </form>

当我更改下拉列表中的值时,我可以看到模式已经更新,但无论我插入什么值,它总是失败。您能否检查并提出可能的原因以及如何解决问题。

像 this.Get 那样做,去掉 seatPattern function.Use 作用域变量。

$scope.vendors = [{"name":"A"},{"name":"B"}];
$scope.seatPattern =   /^[a-cA-C]+[1-8]{1}/;                       
$scope.setVendorName = function(){

if($scope.vendorName.name== 'A')
    { $scope.seatPattern =   /^[a-fA-F]+[1-6]{1}/; }
else
    { $scope.seatPattern =   /^[a-cA-C]+[1-8]{1}/; }

localStorageService.set("vendorName",$scope.vendorName.name);
$scope.vendor = localStorageService.get("vendorName");
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form class="form" name="userForm" novalidate>
    <ul>
      <!--  <li ng-hide="checkRecentOrder()==false">
            <a class="btn btn-primary" href="#">Tract Your Existing Order</a>
        </li>-->
        <li> 
          <select name="vendor" ng-model="vendorName" ng-options="vendor.name for vendor in vendors" class="name" required ng-change="setVendorName()">
              <option value="">Select Audi</option>
          </select>
          <span class="error" ng-show="userForm.vendor.$error.required && userForm.vendor.$dirty" style="color:#db3721; font-weight:normal;">
              <br />Please select one option
          </span>
      </li>
      <li> 
          <input name="seat" 
                 class="name" 
                 placeholder="Seat Number "  
                 ng-model="seat_number" 
                 ng-maxlength="2" 
                 ng-minlength="2"
                 ng-pattern="seatPattern"
                 required/>
          <!--ng-pattern="/^[a-zA-Z]+[0-9]{1}/"-->
          <span class="error" ng-show="userForm.seat.$dirty && userForm.seat.$invalid" style="color:#db3721; font-weight:normal;">

              <span ng-show="userForm.seat.$error.required">
                  <br />Please enter your seat number
              </span>
              <span ng-show="userForm.seat.$error.maxlength || userForm.seat.$error.minlength || userForm.seat.$error.pattern">
                  <br />
                  Please enter seat num alpha numeric (A1)              </span>

          </span>
      </li>


    </ul>  
  </form>

问题出在您的 seatPattern 函数中。

您返回的是字符串,而不是正则表达式。删除引号,它将正常工作。

$scope.seatPattern = function(audi){
     if(audi.name == 'A'){
           return /^[a-fA-F]+[1-6]{1}/;
      }else{
           return /^[a-cA-C]+[1-8]{1}/;
      }
}