我有一个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;
}
})();
答案 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;
}
}
})();