Javascript 范围复选框值未传递到指令?
这是一个使用正则表达式进行简单验证的指令。由于某些原因,我在传递超出指令范围的复选框值时遇到问题。我正在尝试读取名为checkEmailOnBlur的指令中名为scope.validationEnabled的复选框值:Javascript 范围复选框值未传递到指令?,javascript,regex,angularjs,validation,Javascript,Regex,Angularjs,Validation,这是一个使用正则表达式进行简单验证的指令。由于某些原因,我在传递超出指令范围的复选框值时遇到问题。我正在尝试读取名为checkEmailOnBlur的指令中名为scope.validationEnabled的复选框值: app.directive('checkEmailOnBlur', function() { var ABN_REGX = /^(\d *?){3}$/; return { restrict: 'A', require: 'ngModel', li
app.directive('checkEmailOnBlur', function() {
var ABN_REGX = /^(\d *?){3}$/;
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, elm, attr, ctrl) {
if (scope.validationEnabled) {
if (attr.type === 'radio' || attr.type === 'checkbox') return;
elm.unbind('input').unbind('keydown').unbind('change');
elm.bind('blur', function() {
scope.$apply(function() {
if (ABN_REGX.test(elm.val())) {
ctrl.$setValidity('abns', true);
console.log('valid abn');
scope.acn = "1010101";
scope.displayName = "display name";
scope.otherName = "otherName";
scope.description = "desc";
} else {
ctrl.$setValidity('abns', false);
console.log('not valid abn');
scope.acn = "";
scope.displayName = "";
scope.otherName = "";
scope.description = ""
}
});
});
}
}
};
问题:目前,当我选中复选框时,for scope.validationEnabled未设置为TRUE。如何传递复选框值?
这里是一个plnkr参考:当执行链接功能时
作用域。在这种情况下,validationEnabled
将是未定义的,这是falsy
。链接功能不会将事件侦听器附加到blur
在下一次编译HTML之前,link函数不会再次运行,因此勾选该复选框并不重要
将if
移动到事件侦听器函数中:
elm.bind('blur', function() {
if (!scope.validationEnabled) return;
// Code
演示: