angular 并列作用域,作用域不存在上下级关系,$scope.$emit发送时 后退时$rootScope.$on会出现接收多个消息的情况
$rootScope.$on(‘saveDraft‘, function() { $scope.saveDraft(); }); $rootScope.$on(‘previewPosition‘, function() { $scope.previewPosition(); }); $rootScope.$on(‘publishJob‘, function() { $scope.publishJob(); }); $scope.previewPosition = function() { $scope.$emit(‘previewPosition‘); }; $scope.saveDraft = function() { $scope.$emit(‘saveDraft‘); }; $scope.publishJob = function() { $scope.$emit(‘publishJob‘); };
正确方式
$scope.$on(‘saveDraft‘, function() { $scope.saveDraft(); }); $scope.$on(‘previewPosition‘, function() { $scope.previewPosition(); }); $scope.$on(‘publishJob‘, function() { $scope.publishJob(); }); $scope.previewPosition = function() { $rootScope.$broadcast(‘previewPosition‘); }; $scope.saveDraft = function() { $rootScope.$broadcast(‘saveDraft‘); }; $scope.publishJob = function() { $rootScope.$broadcast(‘publishJob‘); };
时间: 2024-11-05 18:59:18