在AngularJS动态电子邮件验证元素中,当我填写电子邮件检查元素时,它's重置
In the AngularJS Dynamic email validation element, When I fill out the email check element, It's reset
这是Angular.JS中的电子邮件检查元素代码几乎可以,但这有问题。当我填写电子邮件检查元素时,它被重置了!
示例我把这个写进电子邮件检查元素。
"test@test.com"
但这是重置!当我写"。"<-指向
"test@test"<-正常
"test@test."<-重置输入。当我写入'.'<-时
为什么会出现问题?
Javascript
<script>
angular.module('test', [])
.controller('MyController', ['$scope', '$window', function($scope, $window) {
$scope.sendMessage=function(toSb){
alert(toSb);
};
}])
.directive('emailInput', function($compile) {
return {
restrict: 'C',
template: '<input type="email" ng-model="emailtext" required>{{emailtext}}',
link: function(scope, elem, attrs){
scope.name="emailAddress";
var input = elem.find("input")[0];
input.name = "emailAddress";
$compile(input)(scope);
}
};
});
</script>
HTML
<form name="myForm">
<div class="email-input"></div>
inputIsValid={{myForm.emailAddress.$valid}}<br>
<span class="error" ng-show="myForm.emailAddress.$error.required">Required!</span>
<span class="error" ng-show="myForm.emailAddress.$error.email">Not valid email!</span>
</form>
您需要使用replace选项生成指令,并从指令元素中获取name属性(基本上不会再次编译输入)。这是一种角度验证,当你编译指令时,它会显示出奇怪的行为,它会在解析后重置modelValue(未定义),但在这种情况下,它也会重置viewvalue。您可以看到,如果您直接使用输入类型,就不会发生这种情况,似乎是重新编译模板导致了这个问题。
.directive('emailInput', function ($compile) {
return {
restrict: 'C',
replace:true,
template: '<input type="email" ng-model="emailtext" required>',
link: function (scope, elem, attrs) {
}
};
和
<div class="email-input" name="emailAddress"></div>
Plnkr
相关文章:
- 如果我在javascript中输入无效的电子邮件或空白,如何显示特定的文本框边框红色
- 使用angularjs验证文本框中的电子邮件
- 使用正则表达式评估电子邮件地址时出现性能问题
- javascript中添加验证电子邮件的问题
- 如何在收到电子邮件时自动调用脚本
- 获取屏幕截图并在电子邮件中发送的按钮
- 将项目履行与高级模板合并,并通过电子邮件发送至Netsuite 2.0
- php&js-将电子邮件添加到输入文本中
- 将用户制作的SVG(raphael.js)发送到服务器(php),以便通过电子邮件发送
- 以PDF附件的形式通过电子邮件发送谷歌文档
- 将Meteor中的base64 PDF作为电子邮件附件
- 来自现场目的的Meteor电子邮件.send()
- 根据部门用户的选择,接收来自多个电子邮件地址的Wordpress Contactform7查询
- 获取电子邮件附件的表单元素
- 根据选择选项元素将表单重定向到不同的URL,并在电子邮件中获取表单详细信息
- 如何访问用PHP/HTML发送的电子邮件的javascript数组中的元素
- 在AngularJS动态电子邮件验证元素中,当我填写电子邮件检查元素时,它's重置
- 最佳实践:在a元素和屏幕阅读器中加密电子邮件地址
- 如何使用jQuery找到具有电子邮件地址作为类名的元素
- 使用电子邮件验证将类添加到 javascript 元素