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

时间:2016-10-28 06:52:46

标签: javascript asp.net angularjs asp.net-mvc

我是AngularJs的新手。任何人都可以帮助在angularJS中使用 http 服务编写工厂,以便工厂返回业务对象而不是承诺,或者在成功时为范围变量赋值。我在网上研究了很多文章,但都使用回调函数或从http服务返回promises。
要求:让我们说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对象,我将使用其他服务

1 个答案:

答案 0 :(得分:0)



<!--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>
&#13;
&#13;
&#13;