角度新手:非平凡的形式验证
Angular newbie: non-trivial form validation?
我是Angular的新手,我想做一些不平凡的输入验证。
基本上我有一张桌子。每行包含三个文本输入。当用户键入任何文本输入时,我想检查表格是否至少包含一行和三个非空白输入字段。如果是这样,我想显示一条消息。
我不知道如何在 Angular 中干净利落地做到这一点,任何帮助将不胜感激。
这是我的 HTML:
<tr data-ng-repeat="i in [1,2,3,4,5]">
<td data-ng-repeat="i in [1,2,3]">
<input ng-model="choice.selected" ng-change='checkAnswer(choice)' type="text" />
</td>
</tr>
...
<div ng-show="haveCompleteRow">we have a complete row!</div>
和控制器:
$scope.haveCompleteRow = false;
$scope.checkAnswer=function(choice){
$scope.haveCompleteRow = true; // what to do here?
}
这是一个演示该问题的 plunker:http://plnkr.co/edit/Ws3DxRPFuuJskt8EUqBB
老实说,我不会称这种形式为验证。但对于初学者来说,如果你有一个真实的模型来观察,而不是模板中的数组,那就简单多了。你开始的方式将,或者至少可以引导你在控制器内部进行dom-manipulation,这是角度的禁忌。
带有模型的简单第一个草图可以是:
app.controller('TestCtrl', ['$scope', function ($scope) {
$scope.rows = [
[{text: ''}, {text: ''}, {text: ''}],
[{text: ''}, {text: ''}, {text: ''}],
[{text: ''}, {text: ''}, {text: ''}]
];
$scope.haveCompleteRow = false;
// watch for changes in `rows` (deep, see last parameter `true`).
$scope.$watch('rows', function (rows) {
// and update `haveCompleteRow` accordingly
$scope.haveCompleteRow = rows.some(function (col) {
return col.every(function (cell) {
return cell.text.length > 0;
});
});
}, true);
}]);
跟:
<body ng-controller="TestCtrl">
<table>
<thead>
<tr>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="row in rows">
<td data-ng-repeat="cell in row">
<input ng-model="cell.text" type="text" />
</td>
</tr>
</tbody>
</table>
<div ng-show="haveCompleteRow">we have a complete row!</div>
</body>
作为模板。
演示:http://jsbin.com/URaconiX/2/
相关文章:
- 正在验证8个真/假复选框或复选框中的2个
- 借助asp.net验证或java脚本对多个文本进行验证
- jQuery自定义验证比较多个输入的序列
- 使用html中的外部javascript进行数据验证
- 如何使用jquery Validation验证Formspread
- jquery中的文本框验证
- 在验证和发送邮件后更改联系人表单的 html
- 代码不会验证
- JS验证ajax返回的html中的表单数据
- 同步调用,直到用户通过angular验证为访问者
- 带有加号的电话号码验证(可选)
- 解析javascript表单验证器
- 两位数的月份日期验证
- 使用angularjs验证文本框中的电子邮件
- 验证Javascript中的Textarea
- 使用regex的jquery keydown绑定不会验证撇号和句点
- javascript验证帮助一个新手
- 角度新手:非平凡的形式验证
- 嗨,我是一个新手在这个Javascript.我想问一下如何验证表单的输入.你能检查一下我的代码吗?
- 我是JavaScript新手.请帮助我的javascript验证