我正在使用asp.net服务器端控件的角度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>
问题是在运行时,当我部署我的代码时,name属性更改为服务器端控件,因此我无法呈现错误消息的段落
还有其他方法可以访问控制而不是myform.txtName。$ invalid吗?
用
clientIDmode= Static
尝试过,它使id保持不变,但名称仍然更改, 最佳答案
尝试此解决方案jsfiddle。
创建指令alias
,该指令存储对ngModelController
的引用,该引用与输入(asp:TextBox)的name
独立。
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>