gpt4 book ai didi

asp.net - Angular JS 和 ASP 服务器端控制验证

转载 作者:行者123 更新时间:2023-12-02 18:27:46 25 4
gpt4 key购买 nike

我正在使用 Angular JS 作为 ASP.NET 服务器端控件

<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的引用,该引用独立于输入(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>

关于asp.net - Angular JS 和 ASP 服务器端控制验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35931924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com