Search code examples
angularjsangularjs-directiveangularjs-controller

Can't communicate from directive to controller. AngularJS 1.6


I've to do little changes in AngularJS project. I've a .js file which first line contains:

(function() { 'use strict';

angular.module( 'tableData' , [] )
  .controller('TableDataController', function( $scope, $routeParams, $window, $modal, $filter, $q, toastr, $document ) { 

This file is inside directives folder. I want to emit an event inside a function (which is triggered when I click in a button, this part is working with a console.debug), that would be caught by a controller.

I've tried with $on.$emit, but I can't get it, so I decided to use $rootScope.$broadcast, but if I pass $rootScope (injection) in .controller('TableDataController', function( $rootScope, ... ), AngularJS shows an error telling that rootScope is not defined.

It doesn't happen if I do it in the controller.

Any idea?

Edit:

I wouldn't need to fires an event if I could call a function from to one to the other file.


Solution

  • I think injection should be like this:

    (function() { 'use strict';
    angular.module( 'tableData' , [] )
        .controller('TableDataController', ['$rootScope', '$scope', '$routeParams',
            '$window', '$modal', '$filter', '$q', 'toastr', '$document',
            function( $rootScope, $scope , $routeParams , 
                $window , $modal , $filter , $q , toastr, $document ) {}
    ])})();
    

    and you can use $rootScope.$broadcast