[英]element.blur() causing Uncaught TypeError: elem.getAttribute is not a function
So I am struggling to understand where a console error is coming from. 因此,我努力了解控制台错误的来源。 It doesn't currently break any behavior, but the error is unsettling.
它目前没有中断任何行为,但是错误令人不安。 The error happens when I trigger a .blur() on a span that is being edited.
当我在正在编辑的跨度上触发.blur()时,会发生错误。 Jquery then complains "jquery.js:1451 Uncaught TypeError: elem.getAttribute is not a function"
jQuery然后抱怨“ jquery.js:1451 Uncaught TypeError:elem.getAttribute不是一个函数”
The directive html: 指令html:
<span ng-if="!time">{{ value }}</span>
<button class="btn btn-primary delink"
ng-if="delinkable && delinkVisable"
ng-click="resource.delink()">Not speaking</button>
<span ng-if="time">{{ value | momentFormat }}</span>
<div ng-if="time && datepicker" class="dropdown" id="datepicker">
<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
<datetimepicker ng-model="resource.MeetingDateTime"
data-on-set-time="updateResource()"
data-datetimepicker-config="{ dropdownSelector: '#dropdown2' }">
</datetimepicker>
</ul>
<a class="dropdown-toggle" id="dropdown2" role="button" data-disabled="true"
data-toggle="dropdown" data-target="#" href>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
</div>
</a>
The relevant link functions: 相关链接功能:
function handleChanges () {
var editSpan = angular.element(element.children()[0]);
var newValue;
if (scope.time) {
newValue = dates.makeDateTime(editSpan.text());
if (newValue) {
scope.resource[scope.property] = newValue;
newValue = editSpan.text();
} else {
editSpan.text(oldValue);
newValue = editSpan.text();
scope.resource[scope.property] = dates.makeDateTime(editSpan.text());
scope.datepicker = true;
$('#dropdown2').trigger('click');
scope.$digest();
}
} else {
scope.resource[scope.property] = editSpan.text();
newValue = editSpan.text();
}
if (newValue !== oldValue) {
scope.updateResource();
compileElement();
} else {
if (scope.time) {
hideDatepicker();
}
}
}
function bindEvents () {
var editSpan = angular.element(element.children()[0]);
editSpan.on('blur', function () {
editSpan.attr('contentEditable', false);
editSpan.unbind('keydown');
editSpan.unbind('blur');
scope.editing = false;
handleChanges();
});
editSpan.on('keydown', function(event){
if (event.keyCode == 27) {
event.preventDefault();
editSpan.text(oldValue);
editSpan.blur(); //The error seems to happen after this call
} else if (event.keyCode == 13) {
editSpan.blur(); //and after this call
} else if (scope.deleteable && event.keyCode == 8) {
if (editSpan.text().length == 0 || event.ctrlKey) {
scope.delete();
}
}
});
}
So I don't know how generalizable this answer will be, but in case it is useful to you, the problem occurred because of the compile function. 因此,我不知道该答案的通用性,但是如果对您有用,那么问题就出在编译功能上。
Basically, compileElement was replacing the originally bound instance of the span with the freshly compiled instance, so when Jquery eventually got around to trying to rebind the click function, the element no longer existed, even though an identical span was there in it's place. 基本上,compileElement用新编译的实例替换了span的原始绑定实例,因此,当Jquery最终尝试重新绑定click函数时,该元素不再存在,即使它所在的范围相同。
The solution was to update the values of the original span, instead of using $compile to re-compile the element. 解决方案是更新原始范围的值,而不是使用$ compile重新编译元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.