javascript - 在 AngularJS 中添加 ng-switch 后,滚动事件未绑定(bind)到 html 元素

标签 javascript jquery html css angularjs

我在我的代码中遇到了一个我无法理解的奇怪错误,可能是范围问题, 在错误的时间绑定(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/

相关文章:

javascript - 如何自动点击按钮(模拟鼠标点击加载)?

javascript - context.createConicGradient 不是函数

javascript - 无法验证 Stripe Payments 的 CSRF token [Rails 4]

javascript - 如何使用客户端 gmail API 发送电子邮件,其中包含 html 内容和图像作为附件

javascript - 我希望一个 <li> 在单击时隐藏另一个 <li> 在同一个 <ul>

html - 传单 map 不会在浏览器中呈现

html - 在 Web 应用程序中禁用 Ipad 上的自动电话检测

c# - 使用文件上传 AJAX 发送帖子数据

php - 在 jQuery ajax 请求中保护和/或加密(隐藏)POST 变量

html - CSS Image Bootstrapping 样式内容