我有一个使用回调函数将内容传递到 Controller 的服务:
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 node module从 csv 文件获取 JSON 格式的内容。这是使用它的 Controller :
angular.module('piApp').controller('homeController', ['$scope', 'dataRetrievalService', function ($scope, dataRetrievalService) {
dataRetrievalService.getContents(function(contents) {
$scope.result = contents;
});
}]);
我想知道如何正确地将解析逻辑移动到注入(inject) dataRetrievalService
的另一个服务,并在解析后仍然将 csv 文件的内容获取到 Controller 。
所以我的新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
}
}])
我正在努力想象这是如何工作的,以便我将第二个回调传递给 Controller 并仍然获得所需的内容。在解析内容后,如何将内容从服务传递到服务到 Controller ?
非常感谢您抽出宝贵的时间。如果您需要任何其他信息或者我不清楚,请告诉我。
这个问题是 this previous post 的延伸
最佳答案
将加载和解析例程分开是个好主意。然而,使用 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;
});
}]);
关于javascript - 解析后如何通过回调将内容从服务传递到另一个服务到 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31842354/