我是 angularjs 新手。
我的表单上有 2 个按钮,一个是“保存”,另一个是“测试连接”按钮。
<td align="left" colspan="0" >
<input class="form-control" title="Specifies the IP address of the SIP trunk ethernet connection." placeholder="xxx.xxx.xxx.xxx"
style="display: inline-block;display:block;white-space: nowrap;overflow: hidden;" type="text"
name="pabxipaddress" id="pabxipaddress" ng-model="userSetup.pabxipaddress" required ng-pattern='patternPresent' >
</td>
<td>
<span class="error" ng-show="(testIPOfficeFlag || submitted) && userSetupForm.pabxipaddress.$error.required">
<label style="color: red;">Required!</label>
</span>
<span class="error" ng-show='(testIPOfficeFlag || submitted) && userSetupForm.pabxipaddress.$error.pattern'>
<label style="color: red;">Invalid IP Address!</label>
</span>
</td>
现在在我的 JS 文件中,当我喜欢时,
$scope.userSetup.pabxipaddress.$valid
对于一些动态测试,它给了我
TypeError: Cannot read property '$valid' of undefined
当我像 $scope.userSetup.pabxipaddress
这样发出警报时,它会正确显示数据。
如何检查单个字段是否正确并通过附加到它的所有约束。
最佳答案
有效属性不是模型值的一部分...尝试
$scope.userSetupForm.postdail.$valid
其中 userSetupForm
是表单的名称,postdail
是 input
元素的名称。
var app = angular.module('my-app', [], function() {
})
app.controller('AppController', function($scope) {
$scope.check = function() {
$scope.validity = {
field1: $scope.myform.myfield1.$valid,
field2: $scope.myform.myfield2.$valid,
field3: $scope.myform.myfield3.$valid
}
};
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="my-app" ng-controller="AppController">
<form name="myform" novalidate>
<div>
<input type="number" name="myfield1" ng-model="formdata.myfield1" required class="numbers-only-for" minvalue="1" maxvalue="45">
</div>
<div>
<input type="text" name="myfield2" ng-model="formdata.myfield2" required>
</div>
<div>
<input type="text" name="myfield3" ng-model="formdata.myfield3" required>
</div>
<button ng-click="check()">Check</button>
</form>
<pre>{{formdata | json}}</pre>
<pre>{{validity | json}}</pre>
</div>
关于javascript - angularjs $valid 不适用于字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27015818/