非常清晰简单的一个小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++;}})});昨天已经解决了.这里把代码发出来方便后面遇到这个问题的朋友吧
添加回答
举报
0/150
提交
取消
