【问题标题】:How to a return business object from factory using http get in angularjs如何在angularjs中使用http get从工厂返回业务对象
【发布时间】:2017-03-11 00:34:30
【问题描述】:

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

    //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);

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

【问题讨论】:

  • 所以你打算阻止整个浏览器等待 http 响应?
  • NO 但我想使用可以返回业务对象的服务。我正在根据从服务返回的 json 对象初始化对象。
  • 你应该在你的服务中加入承诺解析。它可能会起作用。

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


【解决方案1】:

<!--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>

【讨论】:

  • 感谢 Nikhil 的回复。我需要这样的东西 我有一个工厂 App.factory('XmlToJsonSvc', [ '$http', function($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 (data, status) { //console.log('Sucess'); callback(data); }) }} ]);我需要修改这个服务
  • 但是这里你提到了一个回调
  • 你可以在工厂中使用任何东西或任何逻辑,但不能在控制器中使用。你可以在此之上编写任意数量的服务或包装函数,但在控制器中我应该像 var obj = Xmlservice 一样使用。方法名()。
  • so methodName() 会在 http 调用期间阻塞?
  • 这里我没有使用任何承诺。但结果将更新到控制器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 2017-07-15
  • 1970-01-01
  • 1970-01-01
  • 2013-04-20
  • 2016-01-25
相关资源
最近更新 更多