user3241778
user3241778

Reputation: 376

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"
        });
      });
    }
  }
}]);

Upvotes: 0

Views: 54

Answers (1)

Parshwa Shah
Parshwa Shah

Reputation: 341

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"});
        });
    }
}
}]);

Upvotes: 1

Related Questions