如何通过callback后的服务传递内容到另一个服务控制器已被parsing?

我有一个使用callback函数将内容传递给控制器​​的服务:

angular.module('piApp').service("dataRetrievalService", function () { function getContents(callback) { //Converter Class var fs = require("fs"); var Converter = require("csvtojson").Converter; var fileStream = fs.createReadStream("order.csv"); //new converter instance var converter = new Converter({ constructResult: true }); //end_parsed will be emitted once parsing finished converter.on("end_parsed", function (jsonObj) { console.log(jsonObj); //here is your result json object //getResult(jsonObj) callback(jsonObj); }); //read from file fileStream.pipe(converter); } // public api return { getContents: getContents } }) 

该服务使用csvtojson节点模块以csv文件的forms获取内容为JSON。 这里是使用它的控制器:

 angular.module('piApp').controller('homeController', ['$scope', 'dataRetrievalService', function ($scope, dataRetrievalService) { dataRetrievalService.getContents(function(contents) { $scope.result = contents; }); }]); 

我想知道如何正确地将parsing逻辑移动到注入到dataRetrievalService另一个服务,并在parsingdataRetrievalService仍将csv文件的内容传递给控制器​​。

所以我新的csvService

 angular.module('piApp').service("csvService", function () { // public methods function getCsvAsJSON(callback) { //Converter Class var fs = require("fs"); var Converter = require("csvtojson").Converter; var fileStream = fs.createReadStream("Contents/Product Groups/orderTest2.csv"); //new converter instance var converter = new Converter({ constructResult: true }); //end_parsed will be emitted once parsing finished converter.on("end_parsed", function (jsonObj) { console.log(jsonObj); //here is your result json object callback(jsonObj); }); //read from file fileStream.pipe(converter); } // public api return { getCsvAsJSON: getCsvAsJSON } }) 

而我的dataRetrievalService变得像

 angular.module('piApp').service("dataRetrievalService", ['csvService', function (csvService) { function getContents() { csvService.getContents(function (contents) { this.result = contents; //should I be using another callback here instead? }); } // public api return { getContents: getContents } }]) 

我正努力想象这将如何工作,以便我传递第二个callback到控制器,仍然得到所需的内容。 在parsing后,II如何将服务内容传递给控制器​​?

非常感谢您的宝贵时间。 如果您需要任何其他信息或者我不清楚,请告诉我。

这个问题是这个以前的职位的延伸

分开加载和parsing例程是个好主意。 但是,使用promise不是callback,而是使用promise。 在这种情况下,这就是服务的样子:

 angular.module('piApp').service("csvService", function($q) { // public methods function getCsvAsJSON() { var deferred = $q.defer(); var fs = require("fs"); var Converter = require("csvtojson").Converter; var fileStream = fs.createReadStream("Contents/Product Groups/orderTest2.csv"); var converter = new Converter({constructResult: true}); converter.on("end_parsed", function(jsonObj) { deferred.resolve(jsonObj); }); fileStream.pipe(converter); return deferred.promise; } return { getCsvAsJSON: getCsvAsJSON } }); angular.module('piApp').service("dataRetrievalService", ['csvService', function(csvService) { function getContents() { return csvService.getCsvAsJSON(); } return { getContents: getContents } }]); angular.module('piApp').controller('homeController', ['$scope', 'dataRetrievalService', function ($scope, dataRetrievalService) { dataRetrievalService.getContents().then(function(contents) { $scope.result = contents; }); }]);