【腾讯云】云产品限时秒杀,爆款1核2G云服务器,首年99元

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');
    };

 

posted @ 2016-05-11 08:55  happenzh  阅读(892)  评论(0编辑  收藏  举报