非常清晰简单的一个小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()
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
昨天已经解决了. 这里把代码发出来 方便后面遇到这个问题的朋友吧
请查看$scope.$apply $scope.$evalsync $scope.$timeout 几个方法的作用。 另外可以去了解一下 angular的脏值检测和实现的过程。
还有可以 阅读一下这篇文章http://www.bennadel.com/blog/2605-scope-evalasync-vs-timeout-in-angularjs.htm