【问题标题】:Error: [$injector:undef] is occurring in service angularjs错误:服务 angularjs 中发生 [$injector:undef]
【发布时间】:2015-04-27 18:57:41
【问题描述】:

使用服务和 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)
                            });;
                        }
                    }

                });
            }]);

请帮我解决我的问题

谢谢!!

【问题讨论】:

  • 您的工厂代码无效,Factoy 应该只返回公共方法,您是在 forEach 循环中创建它们。是正确的代码,还是无效的复制粘贴?
  • 这是正确的代码..当我在循环内进行控制台时,我得到了值但无法返回它。
  • 所以简而言之,您想为每个 deviceId 调用多个 AJAX 调用并将所有结果汇总到一个列表中? deviceIds 有多少个元素?
  • 2 最大值,这是一个动态值。是的,我需要调用这两个值。
  • 我已经弄清楚了:) 我创建了一个示例代码,它可以帮助您解决问题,并附有一些描述。

标签: angularjs


【解决方案1】:

您的工厂声明无效。

  1. 你的工厂应该只返回一个方法
  2. 创建一个返回 $http 承诺的方法
  3. 聚合 $q 中的所有 Promise,等待所有 Promise 返回响应
  4. 汇总所有响应
  5. 在 promise 中返回它们 - 你不能返回值,因为 AJAX 调用是异步的。

你的工厂应该是这样的:

      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?

更新

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

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

【讨论】:

  • 它向你展示了这个概念,它可能包含一些语法错误,因为我附近没有 IDE :) 我建议一步一步地做,不要在开始时实现整个代码,因为它是复杂:)
  • 出现错误 TypeError: angular.map is not a function on search about google isSuggest angular google map
  • @user2590035 我的错 :) map 是一个 javascript 函数 :) 会添加一些 refs
  • 是的,请..我对此一无所知。
  • 我加了几点,你一步一步做,然后你就可以解决了:)
猜你喜欢
  • 2020-10-27
  • 1970-01-01
  • 2017-10-14
  • 2017-10-14
  • 2015-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-16
相关资源
最近更新 更多