3

I am using angular JS for asp.net server side controls

<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>

problem is at run time when i deploy my code , name property changes for server side control so i am not able to render paragraph of error messages is there any other way through which i can access control instead of myform.txtName.$invalid??

tried with clientIDmode= Static , it keeps id static but name still changes,

1 Answer 1

2

Try this solution jsfiddle.

Create a directive alias, which stores a reference to ngModelController, which is independent of the name of input (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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.