angular $emit发送 后退时$on多次接收

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');
};
上一篇:几分钟搞定redis存储session共享——设计实现


下一篇:canvas连线特效