【发布时间】:2014-10-17 16:14:24
【问题描述】:
我在下面的 plunker 有一个联系人验证指令... http://plnkr.co/edit/9kgZgW?p=info
return {
restrict: 'A',
link: function(scope, elem, attrs, controller) {
var fieldValue;
var checker = function() {
// Get the value of the field type
var dateType = elem.inheritedData("$formController")[attrs.formFieldValidate];
var dataTypeValue = dateType.$viewValue;
// get the field value to be validated against the field type
fieldValue = scope.$eval(attrs.ngModel);
switch (dataTypeValue) {
case "Email":
attrs.$set('placeholder', 'username@example.com');
return (fieldValue && VALID_EMAIL.test(fieldValue)) ? true : false;
case "Phone":
attrs.$set('placeholder', '1234567890 | 123 456 7890 | (123) 456-7890 | 123-456-7890');
return (fieldValue && VALID_PHONE.test(fieldValue)) ? true : false;
case "IM":
attrs.$set('placeholder', 'username@example.com');
return (fieldValue && VALID_IM.test(fieldValue)) ? true : false;
default:
attrs.$set('placeholder', 'Details');
return false;
}
};
scope.$watch(checker, function(valid) {
scope.validField = valid;
return valid ? fieldValue : undefined;
});
}
};
除了检查器功能无限运行的 IE10 和 IE11 之外,代码运行良好。初步调查显示默认开关盒中的 attrs.$set 使其保持运行。结果是浏览器被冻结。
您能否指出,我如何使 scope.$watch 与 attrs.$set 在指令中一起工作?
请注意,该问题仅存在于 IE10 和 IE11 中。
【问题讨论】:
-
尝试使用类似:var unwatch = scope.$watch 并在你完成工作时在 $watch 块内调用 unwatch()
标签: angularjs angularjs-directive angularjs-scope