Angular Js - 从指令中的 $rootScope 发出

Angular Js - emit from $rootScope within directive

我正在尝试打开对话框,它们有自己的控制器,通过事件打开它们。 我现在的问题是,我总是得到

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

如何正确注入 $rootScope

我正在使用 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"
        });
      });
    }
  }
}]);

试试这个

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