为了账号安全,请及时绑定邮箱和手机立即绑定

Angular $document 全局键盘事件不刷新数据.

Angular $document 全局键盘事件不刷新数据.

守着星空守着你 2019-04-16 20:27:20
非常清晰简单的一个小demo但是.angular.module('word',[]).controller('wordCtrl',['$scope','$document',function($scope,$document){$scope.selectNum=1;$document.bind("keypress",function(event){if(event.keyCode==38){$scope.selectNum--;}if(event.keyCode==40){$scope.selectNum++;}console.log($scope.selectNum);});$scope.$watch('selectNum',function(){console.log($scope.selectNum);})$scope.getNum=function(){$scope.$apply();alert($scope.selectNum);}}])$wacth每次按小键盘上下键都可以成功输出selectNum的值,但是$watch不会a检测到selectNum值的变动视图{{selectNum}}也无变化.除非调用一次getNum()
查看完整描述

2 回答

?
繁星coding

TA贡献1797条经验 获得超4个赞

$document.bind("keypress",function(event){
$scope.$apply(function(){
if(event.keyCode==38){
$scope.selectNum--;
}
if(event.keyCode==40){
$scope.selectNum++;
}
})
});
昨天已经解决了.这里把代码发出来方便后面遇到这个问题的朋友吧
                            
查看完整回答
反对 回复 2019-04-16
  • 2 回答
  • 0 关注
  • 452 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号