我在我的代码中遇到了一个我无法理解的奇怪错误,可能是范围问题, 在错误的时间绑定(bind)事件,不确定。
这是场景,我有以下部分。
<ul class="items">
<li></li>
<li></li>
</ul>
在定义的指令中,在 link: 方法中,我将滚动事件绑定(bind)到
- 并且它起作用了。
即
$(".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>
在 Controller 中,绑定(bind)滚动事件的相同代码不再有效, 我尝试将类添加到 ng-switch 并尝试绑定(bind)滚动事件,但没有成功。 我尝试添加一个 div 并添加 ng-switch 作为指令,但没有成功。
这里出了什么问题?
最佳答案
您的绑定(bind)事件可能在 DOM 准备就绪之前被调用,因此您可以使用链接函数的元素参数将事件直接添加到现有元素。
replace: false,
link: function(scope, element, attrs) {
element.scroll(function() {
console.log("scrolling...");
});
}
关于javascript - 在 AngularJS 中添加 ng-switch 后,滚动事件未绑定(bind)到 html 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23038001/