如何从angularjs中的http get服务返回业务对象(控制器中没有回调和承诺功能) [英] How do I return business object from http get service in angularjs (no callback and promise function in controller)
问题描述
App.factory('XmlToJsonSvc',
[ '$http', function($http) {
return {
get : function(path, callback) {
$http.get(path, {
transformResponse : function(data) {
// convert the data to JSON and provide
// it to the success function below
var x2js = new X2JS();
var json = x2js.xml_str2json(data);
return json;
}
}).success(function(data, status) {
//console.log('Sucess');
callback(data);
})
}
}
} ]);
我尝试了什么:
我是AngularJs的新手。任何人都可以帮助在angularJS中使用http服务编写工厂,以便工厂返回一个业务对象而不是promise,或者在成功时为scope变量赋值。我在网上研究了很多文章,但都使用回调函数或从http服务返回promises。要求:假设XMLtoJsonService是我的工厂,它将xml从本地文件夹转换为json。工厂应该返回业务对象,以便在我的控制器中我应该能够以下列方式使用// controller
var obj = XMLtoJsonService.MethodName();
(控制器中不应使用承诺或回调函数)
转换承诺或回调函数的所有逻辑都可以在工厂中使用
What I have tried:
I am new to AngularJs. Could anyone help to write a factory using http service in angularJS so that the factory returns me a business object instead of promise or assigning value to the scope variable on success. I researched so many articles in the web, but all are using either callback function or by returning promises from the http service. Requirement : Let's say XMLtoJsonService is my factory which converts xml to json from the local folder. factory should be return business object so that in my controller I should be able to use in following way //controller
var obj = XMLtoJsonService.MethodName();
(No promises or callback function should be used in controller)
All the logic to convert promise or callback functions can be used in factory
推荐答案
http', function (
http){
return {
get: function ( path,callback){
http) { return { get : function(path, callback) {
http.get(path,{
transformResponse: function (data){
// 将数据转换为JSON并提供
// 它到下面的成功函数
var x2js = new X2JS();
var json = x2js.xml_str2json(data );
return json;
}
})。success( function (数据,状态){
/ / console.log('Sucess');
回调(数据);
})
}
}
}]);
http.get(path, { transformResponse : function(data) { // convert the data to JSON and provide // it to the success function below var x2js = new X2JS(); var json = x2js.xml_str2json(data); return json; } }).success(function(data, status) { //console.log('Sucess'); callback(data); }) } } } ]);
我的尝试:
我是AngularJs的新手。任何人都可以帮助在angularJS中使用http服务编写工厂,以便工厂返回一个业务对象而不是promise,或者在成功时为scope变量赋值。我在网上研究了很多文章,但都使用回调函数或从http服务返回promises。要求:假设XMLtoJsonService是我的工厂,它将xml从本地文件夹转换为json。工厂应该返回业务对象,以便在我的控制器中我应该能够以下列方式使用// controller
var obj = XMLtoJsonService.MethodName();
(控制器中不应使用承诺或回调函数)
转换承诺或回调函数的所有逻辑都可以在工厂使用
What I have tried:
I am new to AngularJs. Could anyone help to write a factory using http service in angularJS so that the factory returns me a business object instead of promise or assigning value to the scope variable on success. I researched so many articles in the web, but all are using either callback function or by returning promises from the http service. Requirement : Let's say XMLtoJsonService is my factory which converts xml to json from the local folder. factory should be return business object so that in my controller I should be able to use in following way //controller
var obj = XMLtoJsonService.MethodName();
(No promises or callback function should be used in controller)
All the logic to convert promise or callback functions can be used in factory
这篇关于如何从angularjs中的http get服务返回业务对象(控制器中没有回调和承诺功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!