如何验证angularjs控制器功能中的多个文本字段的验证。我正在使用角度js 1.5.6 这里有html代码
<div ng-controller="Controller">
<div ng-repeat="data in datas track by $index">
<div class="form-group">
<div class="col-sm-8">
<input type="text" class="form-control input-sm"
name="info[]"
id="info[$index]"
ng-model="sendInformation.info[$index]" required>
</div>
</div>
</div>
<a href="" ng-click="getValue()">Get Value</a>
{{result}}
Ande这里是js代码
myApp.controller('Controller', function($scope) {
$scope.sendInformation = {
info: []
}
$scope.datas = [
{"id":'1'},
{"id":'1'},
{"id":'1'},
];
$scope.getValue = function()
{
if($scope.sendInformation.info == null)
{
$scope.result = "Empty ____ Field";
}
else
{
$scope.result = "Ok";
}
}
});
我做了一个掠夺者:https://plnkr.co/edit/ZvrvzNwMwDSDHfHDrqGs?p=preview 我不明白如何获取动态文本值以验证它是否为空。感谢。
答案 0 :(得分:1)
您只需将其转换为表单即可。使用ng-attr-name="info-{{data.id}}"
为它们指定唯一的名称(例如:如果id为1,则名称将评估为name="info-1"
),然后向该按钮添加ng-disabled
属性,因为我们已添加{{ 1}}在form.$invalid
内,每个输入元素的必需属性,如果为空,则将ng-disabled
设置为true,除非我们填满所有输入框!如下所示。
form.invalid
var myApp = angular.module('app', []);
myApp.controller('Controller', function($scope) {
$scope.errorMessage = "";
$scope.sendInformation = {
info: []
}
$scope.datas = [
{"id":'1'},
{"id":'2'},
{"id":'3'},
];
$scope.getValue = function(form){
var errors = "";
for(var x of form.$error.required){
errors = errors + x.$name + " ";
}
if(form.$invalid){
$scope.errorMessage = "Fields " + errors + "are required!";
}else{
$scope.errorMessage = "";
}
}
});