简体   繁体   English

如何在angularjs控制器中使用来自HTTP请求的数据?

[英]How to use data from http request in angularjs controller?

I'm a code newbie and trying to learn angularjs. 我是代码新手,正在尝试学习angularjs。 My project is simple: get JSON data from an API and display it on a web page. 我的项目很简单:从API获取JSON数据并将其显示在网页上。 I found functions to make the http request and then use the data: 我找到了发出http请求然后使用数据的函数:

 app.factory('myService', function($http) {
   var myService = {
     async: function() {
       var promise = $http.get('<URL to api>').then(function(response) {
         console.log(response);
         return response.data;
       });
       return promise;
     }
   };

   return myService;
 });

 app.controller('getRealTimeBusDataCtrl', function(myService, $scope) {

   myService.async().then(function(d) {
     $scope.data = d;
   });
 });

I can then access and display the whole JSON data chunk or parts of it. 然后,我可以访问并显示整个JSON数据块或其中的一部分。

What I'd like to do is set multiple $scope variables instead of just one, but as soon as I try that, the code breaks. 我想做的是设置多个$ scope变量而不是一个,但是一旦我尝试了,代码就会中断。

What I'm trying to do: 我正在尝试做的是:

if (d.ResponseData.Buses[1].JourneyDirection = 1) {
  $scope.timeToSollentuna = d.ResponseData.Buses[1].DisplayTime;
  else if (d.ResponseData.Buses[1].JourneyDirection = 2) {
    $scope.timeToVallingby = d.ResponseData.Buses[1].DisplayTime;
  } else if (d.ResponseData.Buses[2].JourneyDirection = 1) {
    $scope.timeToSollentuna = d.ResponseData.Buses[2].DisplayTime;
  } else {
    $scope.timeToVallingby = d.ResponseData.Buses[2].DisplayTime;
  }
}

I'm guessing the problem is how the function is set up: that it somehow limits the number of variables I can set or the number of things I can "do" after .then, but I haven't been able to figure out another way to do it. 我猜问题出在函数的设置方式上:它以某种方式限制了我可以设置的变量的数量,或者之后我可以做的事情的数量,但是我无法弄清楚另一个做到的方式。

Sorry if the answer to this question is SO obvious, but I've really tried to find it and failed. 抱歉,这个问题的答案很明显,但我确实试图找到它,但失败了。

Best regards, 最好的祝福,

The way that service is written is unnecessarily verbose. 服务的编写方式不必要地冗长。 Instead, I would rewrite it like this (especially if you're starting out and learning the basics--it's good to learn good habits when starting). 相反,我会这样重写它(尤其是如果您是从入门开始并学习基础知识的话-开始时学习良好的习惯是件好事)。

app.factory('myService', ["$http", function($http){
    return {
        getData: function() {
            return $http.get('path/to/api');
        }
    };
}]);

app.controller('MainCtrl', ["$scope", "myService", function($scope, myService) {
    //declare your data model first
    $scope.busData = undefined;

    //use the service to get data
    myService.getData().then(function(response) {
        //success... got a response
        //this is where you can apply your data
        $scope.busData = response.data;

        //call a function to apply if you'd like
        applyBusData(response.data);
    }).catch(function(response) {
        //error has occurred
    });

    function applyBusData(data) {
        if(data.Buses[1].JourneyDirection === 1) {
            //etcc... etc...
        } else {
            //etc.. etc...
        }
    }
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用angularjs将$ http请求返回的数据用于同一控制器中的另一个请求? - How to use data returned by $http request for another request in same controller using angularjs? 如何使用AngularJS的几个ajax调用($ http)从单个php文件中请求数据? - How to use AngularJS's several ajax call($http) to request data from a single php file? 如何在控制器外使用$ http? 在AngularJS中 - How to use $http outside of a controller? in AngularJS 使用AngularJS控制器的HTTP请求 - Http request using AngularJS controller AngularJS,如何使用服务捕获HTTP数据并将其绑定到我的控制器? - AngularJS, how do I use a service to capture HTTP data and bind it to my controller? AngularJS-如何在控制器中使用通过Factory GET请求检索的数据? - AngularJS - How can I use the data retrieved using a Factory GET request in my controller? AngularJs在控制器初始化之前从$ http获取数据 - AngularJs get data from $http before controller init AngularJS控制器在初始化之前需要来自$ http的数据 - AngularJS controller needs to have data from $http before initialized AngularJs错误:$ http请求虽然在控制器中定义 - AngularJs Error : $http request though defined in controller AngularJs控制器不等待$ http请求 - AngularJs controller doesn't wait for $http request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM