Javascript 指令中关于模糊的角度检查
我是angular js的新手,希望验证模糊上的字段。 当我输入值时,将调用指令并验证输入的值。我希望这一切发生在模糊上。 我已经在html中使用了blur来调用其他函数 以下是指令:Javascript 指令中关于模糊的角度检查,javascript,html,angularjs,Javascript,Html,Angularjs,我是angular js的新手,希望验证模糊上的字段。 当我输入值时,将调用指令并验证输入的值。我希望这一切发生在模糊上。 我已经在html中使用了blur来调用其他函数 以下是指令: app.directive("emailCheck", function(){ return { require: 'ngModel', link: function (scope, elm, attrs, ctrl) { ctrl.$parsers.unshift(functio
app.directive("emailCheck", function(){
return {
require: 'ngModel',
link: function (scope, elm, attrs, ctrl) {
ctrl.$parsers.unshift(function (viewValue, $scope) {
var emailPattern = /\S+@\S+\.\S+/;
var isEmailValid = emailPattern .test(viewValue)
ctrl.$setValidity('emailFormat', isEmailValid);
return viewValue;
})
}
};
});
如何在此处检查模糊事件
提前感谢。您应该将
blur
事件绑定到指令。试着这样做:
link: function (scope, elm, attrs, ctrl) {
elm.bind('blur',function(){
})
}
var-app=angular
.module('MyApp'[
])
.controller('Main',['$scope',函数($scope){
}])
.指令(“emailCheck”,函数(){
返回{
要求:'ngModel',
链接:函数(范围、elm、属性、ctrl){
elm.bind('blur',function(){
警报(“测试”);
})
}
};
});代码>
您应该将blur
事件绑定到指令。试着这样做:
link: function (scope, elm, attrs, ctrl) {
elm.bind('blur',function(){
})
}
var-app=angular
.module('MyApp'[
])
.controller('Main',['$scope',函数($scope){
}])
.指令(“emailCheck”,函数(){
返回{
要求:'ngModel',
链接:函数(范围、elm、属性、ctrl){
elm.bind('blur',function(){
警报(“测试”);
})
}
};
});代码>
您必须在指令中处理模糊事件,并从html元素中删除该事件,以便按以下方式处理它:
app.directive("emailCheck", function(){
return {
require: 'ngModel',
link: function (scope, elm, attrs, ctrl) {
elm.on('blur', function() {
// @TODO you will have to put your code here to handle this event
});
}
};
});
您必须在指令内处理模糊事件,并将其从html元素中删除,以便按以下方式处理:
app.directive("emailCheck", function(){
return {
require: 'ngModel',
link: function (scope, elm, attrs, ctrl) {
elm.on('blur', function() {
// @TODO you will have to put your code here to handle this event
});
}
};
});
按照惯例,当您在指令中使用ngModel时,您并不决定挂接到哪个事件。您可以订阅ngModel连接到的任何事件。默认情况下,这不仅仅是模糊
指令的用户可以将ngmodelpoptions与updateOn:
blur``一起使用,仅在blur上更新,如:
<input
email-check
name="testEmail" ng-model="testEmail"
ng-model-options="{ updateOn: 'blur' }">
<!-- Note using "updateOn: 'blur'" NOT "updateOn: 'default blur'" -->
通常,当您在指令中使用ngModel时,您不会决定要挂接到哪个事件。您可以订阅ngModel连接到的任何事件。默认情况下,这不仅仅是模糊
指令的用户可以将ngmodelpoptions与updateOn:
blur``一起使用,仅在blur上更新,如:
<input
email-check
name="testEmail" ng-model="testEmail"
ng-model-options="{ updateOn: 'blur' }">
<!-- Note using "updateOn: 'blur'" NOT "updateOn: 'default blur'" -->
试试这个
define(['app'],function(app){
app.directive('myDecimals', function() {
return {
restrict: 'A',
link: function(scope, elm, attrs) {
elm.bind('blur',function(){
elm.val(elm.val()*1);
});
}
};
});
});
试试这个
define(['app'],function(app){
app.directive('myDecimals', function() {
return {
restrict: 'A',
link: function(scope, elm, attrs) {
elm.bind('blur',function(){
elm.val(elm.val()*1);
});
}
};
});
});