如何在控制器中使用Angular输入字段电子邮件验证? [英] How to use Angular input fields email validation inside controller?
问题描述
我正在尝试在Angular控制器中将变量验证为电子邮件.
I'm trying to validate a variable as email inside Angular controller.
var valid = $filter('email')($scope.email);
电子邮件过滤器不存在,生成未知的提供者错误".从控制器内部访问电子邮件验证的正确方法是什么?
The email filter doesn't exists, generates "unknown provider error". What's the correct way to access email validation from inside the controller?
谢谢.
以后的不要让我创建自定义滤镜,它必须是使用角度验证的一种方法.
Later edit: don't put me to create a custom filter, it must be a way using angular validation.
推荐答案
您可以创建隐藏的输入类型电子邮件:
You can create a hidden input type email:
<input type="email" hidden id="emailValidator" required ng-model="x" />
然后在js中将值设置为该输入并检查有效性:
Then in js set the value to that input and check validity:
var validator = $('#emailValidator')[0];
validator.value = "someemail@tovalidate.com";
return validator.checkValidity();
此文档也可能会有所帮助: https://docs.angularjs.org/guide/forms
Also this documentation could be helpful: https://docs.angularjs.org/guide/forms
这篇关于如何在控制器中使用Angular输入字段电子邮件验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!