1
的廣播事件後未在視圖中更新我已經閱讀過類似的問題,我無法指出爲什麼這不會更新。我可以在catch事件上獲取範圍變量的日誌。但它沒有更新視圖。所以它填充「測試」,但不會更新$ scope.keyPressed更新時。
指令
app.directive('keypressEvents', [
'$document',
'$rootScope',
function($document, $rootScope) {
return {
restrict: 'A',
link: function() {
$document.bind('keypress', function(e) {
console.log('Got keypress:', e.which);
// console.log('document', $document)
$rootScope.$broadcast('keypress', e);
$rootScope.$broadcast('keypress:' + e.which, e);
});
}
};
}
]);
控制器
$scope.keyPressed = 'test';
$scope.$on('keypress:13', function(onEvent, keypressEvent) {
$scope.keyPressed = 'Enter';
console.log($scope.keyPressed);
});
// For listening to all keypress events
$scope.$on('keypress', function(onEvent, keypressEvent) {
if (keypressEvent.which === 120) {
$scope.keyPressed = 'x';
}
else {
console.log('else' ,keypressEvent);
$scope.keyPressed = 'Keycode: ' + keypressEvent.which;
}
});
<div data-keypress-events>
{{keyPressed}}
</div>
爲視圖控制器由路徑提供程序時加載的部分所指示的視圖。
.when('/feed/:url', {
templateUrl: '/templates/eventWall-feed',
controller: 'eventWallFeedController'
})
謝謝。真正搞砸了我的是我從另一個問題得到了指令片斷。它只是毀了我爲什麼不能更新。爲什麼其他$ scope變量會被更新。就像在ajax調用的回調函數中,我可以更新.then函數中的$ scope變量,並且它會更新。但在這裏我必須打電話給$ apply? –
我覺得因爲$ broadcast和$ on有可能被多次運行,並且如果它每次都運行一個摘要循環,將會妨礙性能。這是我最好的猜測。 – Ronnie