【问题标题】:Using $http request results to present in differen module in Angular使用 $http 请求结果呈现在 Angular 的不同模块中
【发布时间】:2014-10-07 16:41:50
【问题描述】:

我要求用户输入他的详细信息并提交他的表单,该表单调用 $http 请求到服务器并传递数据。

app.service('Request',function($http,$location)
var baseUrl='/system/views/files.json';
    var method='GET';
    var details={}
    this.flightReq=function(){
        $http({
            method:method,
            url:baseUrl,
            headers:{'Content-Type': 'application/x-www-form-urlencoded'},
            params: {'dep':'a', 'arl':'b','number':'c', 'date1':'d'},
            cache:true
        })
        .success(function(data,status){
            console.log(data);
            console.log(status);
            $location.path('/resultpresentation/example');
            details.flightDet=function(){ return data }
        })
        .error(function(data,status){
            console.log(data||"Request failed");
            console.log(status);
            $location.path('/');
        })
    };

如请求所示,一旦成功,用户将被重定向到新位置。 然而,在这个结果页面上,我现在试图展示结果。因此,我必须将结果存储在需要注入其他模块的变量或函数中。 http 请求被注入到两个模块中,但结果应该只被注入到结果模块中。

但是,即使我现在注入服务请求并在控制器中使用$scope.data=Request.flightDet;,我也无法在视图中显示数据。我现在可以看到它出现在控制台中。飞行结果仅在控制台中(成功后但在视图甚至控制器中都无法访问)

但是,我不知道该怎么做?我希望你能帮助我。

最好的

【问题讨论】:

    标签: angularjs http get


    【解决方案1】:

    不建议从service 内部更改路由。相反,您的服务应该只进行$http 调用并将数据交还给调用者。根据来自service 调用的响应数据,controller 必须执行任何进一步的操作。

    但是,您始终可以将 $http.success() 内的数据保存到正在调用的 service 内的局部变量中。只要你把inject这个服务放到任何控制器中,你就可以访问这个数据,因为所有的AngularJS services都是单例的。

    编辑:将数据保存到Request 服务内的变量中,以供任何消费controller 使用。

    app.service('RequestService',function($http,$location){
       var baseUrl='/system/views/files.json';
        var method='GET';
        var flightDetails={};
        this.flightReq=function(){
        $http({
            method:method,
            url:baseUrl,
            headers:{'Content-Type': 'application/x-www-form-urlencoded'},
            params: {'dep':'a', 'arl':'b','number':'c', 'date1':'d'},
            cache:true
        })
        .success(function(data,status){
            console.log(data);
            console.log(status);
            flightDetails = data;
        })
        .error(function(data,status){
            console.log(data||"Request failed");
            console.log(status);
        });
        };
    
        this.getFlightDetails = function(){
            return flightDetails;
        }
    };
    
    app.controller('testController', ['RequestService', function(requestService){
     console.log(requestService.getFlightDetails());
    }]);
    

    【讨论】:

    • 感谢您的回复。我明白了,我将结果存储在details.flightDet=function(){ return data; } 的服务中,并将请求定义为var details={}。我现在可以看到结果成功到达我的结果页面,或者至少它显示在控制台中。但是,即使我现在注入服务Request 并使用$scope.data=Request.flightDet;,我也无法在视图中显示数据。你知道我的意思吗?
    • 只是在服务中做了些改动……并做了一些解释
    • var details = {}service 中创建一个私有变量。您需要通过将其更改为this.details = {} 来公开它。这将解决您的问题。如果它解决了您的问题,请接受答案。
    • 我知道你的意思。我很确定我们非常接近。但是,当使用 this.details 时说“未定义详细信息”。我有种感觉,我在成功回调details.flightDet=function(){ return data } 中的函数没有正确返回并且没有添加到“详细信息”对象中。此外,webstorm 还告诉我它在全局范围内未使用。
    • 你从哪里得到details is not defined?是在您尝试将数据保存在 .success() 中时,还是在您尝试在另一个控制器中访问 service.details 时?
    【解决方案2】:

    鉴于您当前的服务配置,在.success 处理程序中,您可以设置Request.myVar = data,然后在另一个页面/状态上,您将能够访问Request.myVar,只要您在控制器内注入该服务( s)您在该页面/状态上使用的

    【讨论】:

    • 感谢您的帮助。我按照你的建议做了。但是,正如我在上面解释的那样,我仍然遇到问题:/...你知道问题是什么吗?
    猜你喜欢
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 2016-03-11
    • 2011-09-28
    • 2022-01-10
    • 2019-04-07
    相关资源
    最近更新 更多