【问题标题】:Angular Js - emit from $rootScope within directiveAngular Js - 在指令中从 $rootScope 发出
【发布时间】:2018-12-26 20:20:28
【问题描述】:

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

无法读取属性 $emit of undefined`,因为某些原因 我的 $rootScope 未定义。

如何正确注入$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"
        });
      });
    }
  }
}]);

【问题讨论】:

    标签: angularjs angularjs-directive rootscope


    【解决方案1】:

    试试这个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      • 2016-05-30
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      • 2020-04-01
      相关资源
      最近更新 更多