$on and $broadcast in angular

前端 未结 4 2147
庸人自扰
庸人自扰 2020-11-22 15:08

I have a footerController and codeScannerController with different views.

angular.module(\'myApp\').controller(\'footerController\', [\"$scope\", function($s         


        
4条回答
  •  遇见更好的自我
    2020-11-22 15:20

    If you want to $broadcast use the $rootScope:

    $scope.startScanner = function() {
    
        $rootScope.$broadcast('scanner-started');
    }
    

    And then to receive, use the $scope of your controller:

    $scope.$on('scanner-started', function(event, args) {
    
        // do what you want to do
    });
    

    If you want you can pass arguments when you $broadcast:

    $rootScope.$broadcast('scanner-started', { any: {} });
    

    And then receive them:

    $scope.$on('scanner-started', function(event, args) {
    
        var anyThing = args.any;
        // do what you want to do
    });
    

    Documentation for this inside the Scope docs.

提交回复
热议问题