Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
JavaScript
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);
					})
				}
			}
} ]);


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
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900