这样做有风险吗?示例:
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/