Html 使用angularJS验证输入文本
我有一个表单,可以在我的待办事项列表数组中添加项目。 但是,如果输入字段为空,则我的函数将向数组中添加一个空值 验证表单的最佳方法是什么Html 使用angularJS验证输入文本,html,angularjs,Html,Angularjs,我有一个表单,可以在我的待办事项列表数组中添加项目。 但是,如果输入字段为空,则我的函数将向数组中添加一个空值 验证表单的最佳方法是什么 <form name="formaddtodo" ng-submit="todoList.addTodo()"> <input type="text" ng-model="todoList.todoText" ng-minlength="1" size="30"
<form name="formaddtodo" ng-submit="todoList.addTodo()">
<input type="text" ng-model="todoList.todoText" ng-minlength="1" size="30"
placeholder="Voeg nieuwe todo toe">
<input class="btn-primary" type="submit" value="voeg toe">
</form>
目前还没有验证,但我很好奇验证的最佳方式是什么
提前谢谢你
<form name="formaddtodo" ng-submit="todoList.addTodo(formaddtodo.$valid)">
<input type="text" ng-model="todoList.todoText" ng-minlength="1" size="30"
placeholder="Voeg nieuwe todo toe" required>
<input class="btn-primary" type="submit" value="voeg toe">
</form>
todoList.addTodo = function(isValid) {
if(isValid) {
todoList.todos.push({text:todoList.todoText, done:false});
todoList.todoText = '';
}
};
todoList.addTodo=函数(isValid){
如果(有效){
todoList.todos.push({text:todoList.todoText,done:false});
todoList.todoText='';
}
};
我认为上述代码将帮助您验证表单。您可以使用内置的ngRequired指令:
<input type="text" ng-model="todoList.todoText" ng-minlength="1" size="30"
placeholder="Voeg nieuwe todo toe" ng-required="required">
AngularJS为最常见的HTML5输入提供了基本实现
类型:(文本、数字、url、电子邮件、日期、广播、复选框)以及
一些验证指令(必需、模式、最小长度、,
最大长度、最小值、最大值)
ngMessages模块支持向用户显示用于表单验证的消息 在ngMessages中,开发人员被迫依赖诸如ng class和ng show之类的指令来显示这些错误 请跟我来。它可能对您现在和将来使用AngulerJS进行验证很有用
<input type="text" ng-model="todoList.todoText" ng-minlength="1" size="30"
placeholder="Voeg nieuwe todo toe" ng-required="required">