我在将工厂注入另一家工厂时遇到了一些麻烦。
错误是:
`ReferenceError: testDataService is not defined`
我认为这应该很容易解决,但它让我头疼。我似乎对依赖注入中涉及的各种语法存在问题。
在我的主要工厂,我正在进行如下的初始服务电话:
this.treeGridOptions = gadgetInitService.initTreeGridTEST();
到目前为止很好。
然而,在initTreeGridTEST
这是我的testDataService工厂,它只返回一些硬编码选项:
(function () {
'use strict';
angular.module('rage').factory('testDataService', [testData ]);
function testData() {
var service = {
treegridData: treegridData
};
return service;
}
function treegridData() {
return {
"altrows": true,
"sortable": true,
"columnsResize": true,
"editable": true,
"showToolbar": false,
"width": "100%",
"height": 400,
"source": {}
};
}
})();
并尝试注入' testDataService'这里:
(function () {
'use strict';
angular.module('rage').factory('gadgetInitService', ['testDataService', gridHierarchyService]);
function gridHierarchyService(testDataService) {
var service = {
initTreeGrid: initTreeGrid,
initColumnChart: initColumnChart,
initTreeGridTEST: initTreeGridTEST
}
return service;
}
function initTreeGridTEST() {
var myData = testDataService.treegridData();
return myData;
}
})();
你的帮助表示赞赏,
鲍勃
答案 0 :(得分:1)
testDataService
只是函数gridHierarchyService
范围内的一个变量,您试图在定义它的函数之外访问它。只需在工厂构造函数中移动函数initTreeGridTEST
,就可以了。
(function () {
'use strict';
angular.module('rage').factory('gadgetInitService',gridHierarchyService);
//Just used it instead of array notation, i prefer this since this is more readable
gridHierarchyService.$inject = ['testDataService'];
function gridHierarchyService(testDataService) {
//yeah keep service it in a variable incase you need to use service object for referring public functions as is. I am just returning it.
return {
initTreeGrid: _initTreeGrid,
initColumnChart: _initColumnChart,
initTreeGridTEST: _initTreeGridTEST
}
function _initTreeGridTEST() {
var myData = testDataService.treegridData();
return myData;
}
}
})();