0

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.

Aleksey Solovey
  • 4,153
  • 3
  • 15
  • 34
  • post more of your code. your requirement isn't very clear with this – Sachet Gupta Aug 01 '18 at 09:06
  • I think there is a typo, perhaps you used `rootScope` instead of `$rootScope`. Or if you have a proper injection you forgot to add it: `TableDataController.$inject = ["$rootScope", ...]` – Aleksey Solovey Aug 01 '18 at 09:59
  • The question is unclear. Do you want to fix the problem with $scope/$rootScope emitting events? Or do you want to communicate from directive to controller without $scope/$rootScope events? When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. That code should be… **Complete** – Provide all parts needed to reproduce the problem. **Include the code for the directive.** Also include an example of the HTML that uses the directive. – georgeawg Aug 01 '18 at 12:44
  • Possible duplicate of [angularjs: broadcast from directive to controller](https://stackoverflow.com/questions/19002092/angularjs-broadcast-from-directive-to-controller). See the [DEMO on Fiddle](http://jsfiddle.net/Kvt4s/). – georgeawg Aug 01 '18 at 13:07

1 Answers1

0

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

JustGentle
  • 109
  • 9
  • I don't recommend switching dependency injection from [implicit annotation](https://docs.angularjs.org/guide/di#implicit-annotation) to [array annotation](https://docs.angularjs.org/guide/di#inline-array-annotation). Array annotation is error prone and violates the [DRY principle](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself). – georgeawg Aug 01 '18 at 13:15