我在我的代码中遇到了一个奇怪的bug,我不能理解,可能是范围问题,事件绑定在错误的时间,不确定。
这是一个场景,我有下面的部分。
<ul class="items">
<li></li>
<li></li>
</ul>在定义的指令中,在link: method中,我将scroll事件绑定到,它就可以工作了。即
$(".items").bind("scroll", function(){
console.log("scrolling...")
});在添加ng-switch之后,
<ng-switch on="show">
<ul class="items" ng-switch-when="true">
<li></li>
<li></li>
</ul>
<ul class="another_items" ng-switch-when="false">
<li></li>
<li></li>
</ul>
</ng-switch>在控制器中,绑定scroll事件的相同代码不再起作用,我尝试向ng-switch添加类,并尝试绑定scroll事件,但不成功。我尝试添加div并将ng-switch作为指令添加,但没有成功。
这里出了什么问题?
发布于 2014-04-13 11:48:24
可能在DOM准备好之前就调用了绑定事件,因此您可以使用link函数的element参数将事件直接添加到现有元素中。
replace: false,
link: function(scope, element, attrs) {
element.scroll(function() {
console.log("scrolling...");
});
}https://stackoverflow.com/questions/23038001
复制相似问题