Angular JS 和 ASP 服务器端控件验证

Angular JS and ASP Server Side Control Validation

我正在为 asp.net 服务器端控件使用 angular JS

<div class="gridpage mb40" name="myForm" ng-model="myForm" ng-form
   <div class="form-group">
      <label class="">Name of the Supplier:</label>
        <asp:TextBox ID="txtSupName" CssClass="form-control" runat="server" ng-model="txtSupName" name="txtName" required></asp:TextBox>
        <p ng-show="myForm.txtName.$invalid && !myForm.txtName.$pristine" class="help-block">Your Name is Required.</p>
   </div>
 </div>

问题出在 运行 我部署代码时,服务器端控件的名称 属性 更改,因此我无法呈现错误消息段 除了 myform.txtName.$invalid??

之外,还有其他方法可以让我访问控制权吗?

尝试使用 clientIDmode= Static ,它保持 id 静态但名称仍然改变,

试试这个解决方案 jsfiddle

创建指令 alias,它存储对 ngModelController 的引用,它独立于输入 name (asp:TextBox)。

var myApp = angular.module('myApp', []);

myApp.controller('MyCtrl', function($scope, $log) {

  })
  .directive('alias', function() {
    return {
      restrict: "A",
      require: "ngModel",
      scope: {
        alias: "="
      },
      link: function(scope, elem, attr, ngModel) {
        scope.alias = ngModel;
      }
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="MyCtrl">
    <form name="myForm">
      <input name="txtName" ng-model="txtName" required alias="aliasName" ng-pattern="/^[0-9]+$/">
      <div ng-if="myForm.txtName.$error.required">Name is reauired by name</div>
      <div ng-if="aliasName.$error.required">Name is reauired by alias</div>
      <div ng-if="myForm.txtName.$error.pattern">Invalid pattern by name</div>
      <div ng-if="aliasName.$error.pattern">Invalid pattern by alias</div>
      <br>
      <pre>myForm.txtName.$error = {{myForm.txtName.$error|json}}</pre>
      <pre>aliasName.$error = {{aliasName.$error|json}}</pre>
    </form>
  </div>
</div>