如何通过从服务内容到另一个服务与回调到控制器已经被解析后? [英] How to pass content from service to another service to controller with callbacks after it has been parsed?
问题描述
我有一个使用一个回调函数来传递内容控制器的服务:
I have a service that uses a callback function to pass content to a 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节点模块获得从CSV文件作为JSON内容。这里是使用它的控制器:
This service uses the csvtojson node module to get content from a csv file as JSON. And here is the controller that uses it:
angular.module('piApp').controller('homeController', ['$scope', 'dataRetrievalService', function ($scope, dataRetrievalService) {
dataRetrievalService.getContents(function(contents) {
$scope.result = contents;
});
}]);
我不知道我怎么会解析逻辑正确地移动到注入 dataRetrievalService
和之后仍然得到了CSV文件的内容到控制器的另一个服务已经被解析。
I'm wondering how I would properly move the parsing logic to another service that is injected into dataRetrievalService
and still get the contents of the csv file to the controller after it has been parsed.
所以我的新的 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
}
}])
我努力想象如何做到这一点的工作,这样我通过第二个回调到控制器,仍然获得所需的内容。我怎么能我通过从服务内容后,它已经被解析,以服务控制器?
I'm struggling to picture how this would work so that I pass a second callback to the controller and still get the desired content. How can I I pass the content from service to service to controller AFTER it has been parsed?
非常感谢您的宝贵时间。让我知道如果你需要任何额外的信息,或者如果我是不清楚。
Thank you very much for your time. Let me know if you need any additional information or if I am being unclear.
这问题是<一个延伸href=\"http://stackoverflow.com/questions/31837062/how-to-pass-content-to-controller-from-service-after-it-is-done-being-parsed\">this previous帖子
推荐答案
这是分离加载和分析例程不错的主意。然而,而不是回调它更方便使用的承诺。这是怎样的服务可能看起来像在这种情况下:
This is good idea to separate loading and parsing routines. However, instead of callback it's more convenient to use promises. This is how services could look like in this case:
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;
});
}]);
这篇关于如何通过从服务内容到另一个服务与回调到控制器已经被解析后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!