如何在angularjs中使用http get从工厂返回业务对象

How to a return business object from factory using http get in angularjs

本文关键字:get 工厂 返回 对象 业务 http angularjs      更新时间:2023-09-26

我是AngularJs新手。有没有人可以帮助在angularJS中使用http服务编写一个工厂,以便工厂返回给我一个业务对象,而不是承诺或在成功时为范围变量赋值。我在网上研究了很多文章,但所有的文章都使用回调函数或从http服务返回承诺。
需求:假设XMLtoJsonService是我的工厂,它将xml从本地文件夹转换为json。工厂应该是返回业务对象,以便在我的控制器中,我应该能够以以下方式使用

    //controller 
    var obj = XMLtoJsonService.MethodName(); 
**(No promises or callback function should be used in controller)** 
/*******service code****************/
    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);
                        })
                    }
                }
    } ]);
/*********Controller Code **********/
var setData = function(data) {
            return new Menus(data);
            debugger
        }
        var path = "Configs/Config.xml";
        XmlToJsonSvc.get(path, setData);

//代码运行正常//但我的要求是将这段代码转换为在我的控制器中//像var obj = XmlToJsonSvc.get(path)//obj应该有json对象,我将使用另一个服务

<!--In this funcion calling factory function restCall() from controller.
before that i have assigned a already created factory object to a scope variable in controller
.So whenever factory varaible updates,that update will be available to controller automatically.
Some kind of 2-way binding method from factory object-->
<!DOCTYPE html>
<html lang="en"  ng-app="app">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body ng-controller="myCtrl">
  {{obj.data1}}
  
  <button ng-click="clickFn()">Clicking this will call service</button>
<script>
    (function() {
      var app = angular.module("app",[])
      
      .controller("myCtrl",function(myFactory,$scope){
        
        $scope.obj = myFactory.bObject;
        
        $scope.clickFn  = function(){
          myFactory.restCall();
        }
        
         
      }).factory("myFactory",function($timeout){
        
        var bObject = {
          data1 :null
        };
        function restCall(){
          $timeout(function(){
            bObject.data1 = bObject.data1 ? false : true ;
          },1000);
        }
        
        var service = {
          bObject : bObject,
          restCall : restCall,
        }
        return service;
        
      });
    }());
</script>
</body>
</html>