【问题标题】:How to listen for a site-wide event in a directive with isolate scope如何在具有隔离范围的指令中侦听站点范围的事件
【发布时间】:2014-07-02 20:02:51
【问题描述】:

我正在使用具有隔离范围的指令在 angularjs 中创建自定义图表小部件。这个想法是每个小部件在收到有关如何创建自己的基本配置信息后应该能够独立存在。

小部件将通过侦听“update.data”事件与应用程序的其他部分进行通信。触发事件时,小部件会根据可用的新配置信息(通过事件对象传递)刷新其数据(调用服务器)。

创建的示例小部件如下

 ng.directive('metricOverview', ['Report', function(Report) {
        return {
            restrict: 'EA',
            //replace: true,
            scope: {
                title: '@',
                metrics: '=',
                report: '@'
            },
            templateUrl: 'scripts/widgets/metric-overview/metric-overview.html',
            controller: function($scope, $element)
            {
            },
            link: function(scope, element, attrs) {
                scope.$on("update.data", function()
                {
                    Report.overview({metric: scope.metric, report: scope.report})
                            .$promise.then(function(result) {
                                console.log(result);

                                $(document).trigger("chart.refresh", result);

                            });
                });

            }
        };
    }]);

我的问题是在哪里触发“update.data”事件最合适。例如,当页面加载并且 DOM 准备就绪时,我希望触发此事件,并且所有加载的小部件都应该能够侦听它并自行更新。我无法触发 rootScope 上的事件,因为它在指令的隔离范围内不可访问。

做了一些研究,但我发现的大部分内容都依赖于使用全局/父范围,由于指令的孤立性质,这不能满足我的目的

解决此问题的最佳方法是什么?

【问题讨论】:

    标签: angularjs angularjs-directive angularjs-scope


    【解决方案1】:

    您好,使用使用 rootscope 和 $emit 的事件总线服务来处理这种情况,这样事件就不会进入很多子范围。你可以把这个服务注入到指令中并添加一个事件监听器。

    (function (angular) {
    'use strict';
    
    /**
     * @ngdoc service
     * @name coreModule.eventbus
     * @requires $rootScope
     *
     * @description
     * Provides a eventing mechanism when a user cna broadcast and subscribe to application wide events.
     */
    angular.module('coreModule').factory('eventbus', [
        '$rootScope',
        function ($rootScope) {
            /**
             * @ngdoc function
             * @name subscribe
             * @methodOf coreModule.eventbus
             *
             * @description
             * Subscribes a callback to the given application wide event
             *
             * @param {String} eventName The name of the event to subscribe to.
             * @param {Function} callback A callback which is fire when the event is raised.
             * @return {Function} A function tht can be called to unsubscrive to the event.
             */
            var subscribe = function (eventName, callback) {
                    return $rootScope.$on(eventName, callback);
                },
    
                /**
                 * @ngdoc function
                 * @name broadcast
                 * @methodOf coreModule.eventbus
                 *
                 * @description
                 * Broadcasts the given event and data.
                 *
                 * @param {String} eventName The name of the event to broadcast.
                 * @param {object} data A data object that will be passed along with the event.
                 */
                broadcast = function (eventName, data) {
                    $rootScope.$emit(eventName, data);
                };
    
            return {
                subscribe: subscribe,
                broadcast: broadcast
            };
        }
    ]);
    }(angular));
    

    【讨论】:

      猜你喜欢
      • 2019-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      • 1970-01-01
      • 1970-01-01
      • 2016-05-03
      相关资源
      最近更新 更多