angularjsangularjs-directiverootscope

Angular Js - emit from $rootScope within directive


I am trying to open dialogs, which have their own Controllers, opening them through events. My problem now is, that I am always getting

Cannot read property $emit of undefined`, because for some reason my $rootScope is undefined.

How can I inject the $rootScope properly?

I am using Angular 1.6.7.

.directive("opendialog", [function($rootScope) {
  return {
    link: function(scope, element, attributes) {
      element.bind("click", function(event) {
        var dialogId = $(element).attr("id");
        $rootScope.$emit(dialogId, {
          command: "open"
        });
      });
    }
  }
}]);

Solution

  • Try this

    .directive("opendialog", ["$rootScope", function ($rootScope) {
    return {
        link: function (scope, element, attributes) {
            element.bind("click", function (event) {
                var dialogId = $(element).attr("id");
                $rootScope.$emit(dialogId, {command: "open"});
            });
        }
    }
    }]);