Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在angularjs控制器中使用http请求中的数据?_Javascript_Angularjs - Fatal编程技术网

Javascript 如何在angularjs控制器中使用http请求中的数据?

Javascript 如何在angularjs控制器中使用http请求中的数据?,javascript,angularjs,Javascript,Angularjs,我是一名代码新手,正在努力学习angularjs。我的项目很简单:从API获取JSON数据并将其显示在网页上。我找到了发出http请求然后使用数据的函数: app.factory('myService', function($http) { var myService = { async: function() { var promise = $http.get('<URL to api>').then(function(response) {

我是一名代码新手,正在努力学习angularjs。我的项目很简单:从API获取JSON数据并将其显示在网页上。我找到了发出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;
   });
 });
我猜问题在于函数是如何设置的:它以某种方式限制了我可以设置的变量的数量,或者限制了我可以“做”的事情的数量。然后,我还没有找到另一种方法来实现它

很抱歉,如果这个问题的答案是如此明显,但我真的试图找到它,但失败了


最好的祝愿,

服务的编写方式是不必要的冗长。相反,我会这样重写它(特别是如果你刚开始学习基础知识的话——在开始的时候学习好习惯是很好的)


使用if语句时,请使用双相等或三相等(
=
==
)而不是单相等(
=
)。请参阅:当“代码中断”时,您会遇到什么错误?感谢您指出=和==,==!
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;
  }
}
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...
        }
    }
}]);