Using $emit in angular 1.5 component

后端 未结 3 437
悲&欢浪女
悲&欢浪女 2021-01-04 03:35

I am using angular 1.5 component and need to call function in parent controller from when $emit in child component. How we can do this?

Example:

(fu         


        
相关标签:
3条回答
  • 2021-01-04 03:42

    I prefer working with a separate event service that exposes subscribe and notify functions. But if you prefer to emit from the child component then it would look like this:

    Child Component

        (function (angular) {
        'use strict';
    
          function childController($scope) {
           $scope.$emit("someEvent", args); 
          }
    
          angular.module('childModuleName', [
          ]).component('childComponent', {
            templateUrl: 'templateUrl',
            controller: ['$scope', childController]
          });
    
        })(angular);
    

    Parent Component

        (function (angular) {
        'use strict';
    
    
          function controllerName($scope) {
             var _this = this;
    
             function toBeCalledOnEmit(event, args) {
                //some code
             }
             $scope.on('someEvent', toBeCalledOnEmit);
    
             var vm = {
                toBeCalledOnEmit: toBeCalledOnEmit
             }
             angular.extend(_this, vm);
          }
    
          angular.module('moduleName', [
          ]).component('parentComponent', {
              templateUrl: 'templateUrl',
              controller: ['$scope', controllerName]
          });
    
        })(angular);
    
    0 讨论(0)
  • 2021-01-04 03:51

    Code is attached below:

    1. Child Component:

      (function (angular) {
          'use strict';
      
      childController.$inject = ['$scope'];
      
      function childController($scope) {
      
         //needs $emit here
      $scope.$emit("topic-123", 'any message'); 
      }
      
      angular.module('childModuleName', [
      ]).component('childComponent', {
          templateUrl: 'templateUrl',
          controller: 'childController'
      }).controller('childController', childController);
      

      })(angular);

    2. Parent Component:

      (function (angular) {
      'use strict';
      
      controllerName.$inject = ['$scope'];
      
      function controllerName($scope) {
         var _this = this;
         function toBeCalledOnEmit() {//some code}
         var vm = {
            toBeCalledOnEmit: toBeCalledOnEmit
         }
      
         $scope.$on('topic-123', function(event, msg) {
        // @TODO  
        toBeCalledOnEmit();
         });
      
         angular.extend(_this, vm);
      }
      
      angular.module('moduleName', [
      ]).component('parentComponenet', {
          templateUrl: 'templateUrl',
          controller: 'controllerName'
      }).controller('controllerName', controllerName);
      

      })(angular);

    0 讨论(0)
  • 2021-01-04 03:53

    You can do it like this using $rootScope. It works fine in my case -

    child component:

    (function (angular) {
     'use strict';
    
     childController.$inject = ['$rootScope'];
    
     function childController($rootScope) {
       $rootScope.$emit('myEvent',$scope.data)
     }
    })(angular);
    

    Parent Component:

    (function (angular) {
     'use strict';
    
     controllerName.$inject = ['$rootScope'];
    
     function controllerName($rootScope) {
       var _this = this;
       function toBeCalledOnEmit() {//some code}
       var vm = {
          toBeCalledOnEmit: toBeCalledOnEmit
       }
       $rootScope.$on('myEvent', function(event, msg) {
         toBeCalledOnEmit();
       });
       angular.extend(_this, vm);
     }
    })(angular);
    
    0 讨论(0)
提交回复
热议问题