将Angular服务注入工厂的问题

时间:2015-12-03 22:35:34

标签: angularjs

我有一个AngularJS工厂,我正在注入一项服务。

我在Chrome中调试时可以看到服务正在注入(在return service;行),但在我的一个函数中无法进一步访问它。

请参阅我尝试调用updateWidget

的函数reportsContext.buildAggrFuncFromKriGrid

(function() {
    'use strict';
    angular.module('app').factory('widgetLinkingFactory', ['$q', 'reportsContext', linking]);

    function linking($q, reportsContext) {

        var service = {
            linkCharts: linkCharts
        };

        return service;

        function linkCharts(parId, widgets, parentWidgetData) {

            _.each(widgets, function(wid) {
                if (wid.dataModelOptions.linkedParentWidget) {
                    updateWidget(wid, parentWidgetData);
                }
            });
        }
    }

    function updateWidget(widget, parWidData) {

        // PULL RISK MEASURES ON CHILD WIDGET 
        var rm = widget.dataModelOptions.riskMeasures;

        // ******* WHY IS 'reportsContext' SERVICE IS UNDEFINED HERE ?????
        var aggrFuncArray = reportsContext.buildAggrFuncFromKriGrid(rm);

        var cubeVectors = aggrFuncArray[0];
        var aggrFunc = aggrFuncArray[1];

        return true;
    }
})();

1 个答案:

答案 0 :(得分:2)

您需要将updateWidget功能移至linkCharts,以便reportsContext可在此处使用:

(function() {
    'use strict';
    angular.module('app').factory('widgetLinkingFactory', ['$q', 'reportsContext', linking]);

    function linking($q, reportsContext) {

        var service = {
            linkCharts: linkCharts
        };

        return service;

        function linkCharts(parId, widgets, parentWidgetData) {

            _.each(widgets, function(wid) {
                if (wid.dataModelOptions.linkedParentWidget) {
                    updateWidget(wid, parentWidgetData);
                }
            });
        }

        function updateWidget(widget, parWidData) {

            // PULL RISK MEASURES ON CHILD WIDGET 
            var rm = widget.dataModelOptions.riskMeasures;

            var aggrFuncArray = reportsContext.buildAggrFuncFromKriGrid(rm);

            var cubeVectors = aggrFuncArray[0];
            var aggrFunc = aggrFuncArray[1];

            return true;
        }
    }

})();