码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-05-11 09:33:29      阅读:370      评论:0      收藏:0      [点我收藏+]

标签:

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

 

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

标签:

原文地址:http://www.cnblogs.com/happen-/p/5480647.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!