错误:[$ injector:undef]正在服务angularjs中发生

时间:2015-04-27 10:11:35

标签: angularjs

使用服务和http时收到错误Error: [$injector:undef]。在更改为此特定错误建议的所有更改后,我无法找到原因。请检查以下代码

mainApp.controller('deviceDataController',["$scope","mainService", function ($scope,mainService) {
                console.log(mainService);
                mainService.deviceDataVar().success(function (data) {
                   // $scope.deviceDetails = mainService.devices;
                   console.log(data);
                });
            }]);
            
                
            mainApp.factory("mainService",["$http", function ($http) {

                angular.forEach(deviceIds, function(value, key) {
                    var timezone = user_response.timezone;
                    var DataUrl = LAX_API + "device_info.php?deviceid=" + value + "&limit=288&timezone=" + timezone;

                    return {
                        deviceDataVar: function () {
                           return $http.get(DataUrl).success(function (response) {
                                devices = response.data;
                                return devices;
                            }).error(function (data, status, headers, config) {
                                // log error
                                console.log(data)
                            });;
                        }
                    }

                });
            }]);

请帮我解决我的问题

谢谢!

1 个答案:

答案 0 :(得分:1)

您的工厂声明无效。

  1. 您的工厂应该只返回单一方法
  2. 创建一个返回$ http promises
  3. 的方法
  4. 在$ q中聚集所有承诺,等待所有承诺返回回复
  5. Agregate所有回复
  6. 在promise中返回它们 - 你不能返回值,因为AJAX调用是异步的。
  7. 你的工厂应该是这样的:

          mainApp.factory("mainService", function ($http, $q) {
                    /* generate single link for deviceID */
                    function getDevice(value){
                       var timezone = user_response.timezone;
                       var dataUrl= LAX_API + "device_info.php?deviceid=" + value + "&limit=288&timezone=" + timezone; 
                       return $http.get(dataUrl);
                     }
    
                    function deviceDataVar(){
                       // map method will transform list of deviceIds into a list of $http promises
                       var allRequests = deviceIds.map(getDevice);
                       // this promise will wait for all calls to end, then return a list of their results
                       $q.all(allRequests).then(function(arrayOfResults) { 
                       // here you will have all responses, you just need to agregate them into single collection.
                       // secondly you will need to wrap them into a promise and return
                       return ...
                     });
    
                   }
    
                      /* Return only public methods, at the end */
                    return {
                        deviceDataVar: deviceDataVar
                    }
            });
    

    (我在飞行中写这个,所以可能会有很少的错误:​​),但是概念是正确的)

    有用的链接:

    1. 汇总承诺:angular -- accessing data of multiple http calls - how to resolve the promises
    2. Array.prototype.map:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
    3. 如何向工厂发出承诺:How can I pass back a promise from a service in AngularJS?
    4. <强>更新

      要通过调整小步骤使其工作,您应该:

      1. 模拟工厂方法,让它返回硬编码值。通过承诺归还。
      2. 将硬编码值替换为选定设备的单个(硬编码)URL。
      3. 对此单个$ http电话使用聚合($ q.all)。
      4. 用deviceIds列表中的数组替换单个$ http。