理想情况下,您不应该将无效值发送到服务器,因此您应该disable\hide
提交按钮,但如果您确实需要将无效值也发送到服务器,那么从angularjs 1.3+ 开始,您有ng-model-options
(Read Doc)指令可以帮助您。
只需将您的text type
输入标记为ng-model-options="{allowInvalid: true }"
,它也会保留无效值。
见演示:
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', function MyCtrl($scope) {
$scope.submitt = function() {
alert($scope.Configure3gCtrl.configure3g.apn);
}
$scope.Configure3gCtrl = {
configure3g: {
apn: ""
}
}
});
<script src="https://code.angularjs.org/1.3.1/angular.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<form name="frm" ng-submit="submitt()" class="row">
<label class="col-sm-2 label-on-left">APN</label>
<div class="col-sm-7">
<div class="form-group label-floating">
<label class="control-label"></label>
<input class="form-control" type="text" name="apn"
ng-model="Configure3gCtrl.configure3g.apn"
ng-model-options="{allowInvalid: true }"
ng-pattern="/^[a-zA-Z0-9-.]*$/" required/>
</div>
</div>
<input type="submit" value="submit" type="submit" />
</form>
</div>
此外,ng-model-options="{allowInvalid: '$inherit' }"
从上面的代码片段中删除的测试ng-model
将是undefined
,因为它是无效的。