jquery - .unbind 当之前没有进行过 .bind 操作时

标签 jquery angularjs

这样做有风险吗?示例:

if (someCondition) {
   angular.element($window).bind('scroll', myHandler);
}

$scope.$on('$destroy', function() {
    angular.element($window).unbind('scroll', myHandler);
});

我当然可以

$scope.$on('$destroy', function() {
    if (someCondition) {
        angular.element($window).unbind('scroll', myHandler);
    }
});

但是不知道有没有必要。我已经在没有 someCondition 条件的情况下进行了测试,并且“它有效”,但我想确定一下。

最佳答案

绝对没问题,销毁范围时无需检查条件。

if (someCondition) {   angular.element($window).bind('scroll', myHandler);}

$scope.$on('$destroy', function() { angular.element($window).unbind('scroll', myHandler);});

关于jquery - .unbind 当之前没有进行过 .bind 操作时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32605630/

相关文章:

javascript - 保存后在数据库中输入 ="date"无效值

javascript - 如何获取当前 ui-router 状态的完整 url?

javascript - 带有 DataTable 自定义搜索框的 AngularJs

jquery - 下拉菜单不起作用

jquery - 使用 jQuery 循环输入字段以找到最高值?

javascript - 如何使用jquery限制用户不允许低于18岁?

jquery - 防止隐藏div中的元素为 'clickable'

javascript - Chosen - 在 Chosen 应用于选择后绑定(bind)到更改事件

javascript - 从 AngularJS 服务销毁事件监听器

javascript - 将图表工具提示自定义为带有指向饼图段的线的标签