【问题标题】:Scroll event is not binding to a html element after adding ng-switch in AngularJS在 AngularJS 中添加 ng-switch 后,滚动事件未绑定到 html 元素
【发布时间】:2014-04-13 11:48:24
【问题描述】:

我的代码中遇到了一个我无法理解的奇怪错误,可能是范围问题, 在错误的时间绑定事件,不确定。

这是场景,我有以下部分。

<ul class="items">
  <li></li>
  <li></li>
</ul>

在定义的指令中,在链接:方法中,我将滚动事件绑定到

    并且它可以工作。 即
$(".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>

在控制器中,绑定滚动事件的相同代码不再起作用, 我尝试将类添加到 ng-switch 并尝试绑定滚动事件,但没有成功。 我尝试添加一个 div 并添加 ng-switch 作为指令,但没有成功。

这里出了什么问题?

【问题讨论】:

  • 能分享一下指令的代码吗?

标签: javascript jquery html css angularjs


【解决方案1】:

您的绑定事件很可能在 DOM 准备好之前被调用,因此您可以使用链接函数的 element 参数将事件直接添加到现有元素。

replace: false,
link: function(scope, element, attrs) {
  element.scroll(function() {
    console.log("scrolling...");
  });
}

【讨论】:

    猜你喜欢
    • 2014-09-17
    • 1970-01-01
    • 2015-10-18
    • 2013-03-13
    • 2017-10-21
    • 2011-09-18
    • 1970-01-01
    • 2022-12-14
    相关资源
    最近更新 更多