【问题标题】:$http get not resolving in $stateProvider$http 在 $stateProvider 中无法解析
【发布时间】:2015-11-27 11:15:19
【问题描述】:

我正在尝试获取路线(app.units)的数据(单位)。这是代码。

  .state('app.units', {
    url: '/units',
    views: {
      'menuContent' :{
        templateUrl: "templates/units.html",
        controller: 'UnitCtrl',
        resolve:{
          units: function($stateParams, ExamService, $rootScope, $http){
            return $http.get(ApiEndpoint.url +'/units.json?auth_token='+$rootScope.globals.currentUser.userToken)
              .success(function(response){
                //console.log('hello');
                return response.data;
              })
          }
        }
      }
    }
  })

正如您所见,这无法解决,不会在控制台中显示任何错误,并在我的 ionic 应用程序中为我提供了一个空白页面,但该端点适用于 chrome 的 Advanced Rest Client。我一直被困在这个问题上,有人可以解决这个问题。

【问题讨论】:

  • 可能$http 调用失败。在 ui-router 和 resolves 的情况下什么都不会显示。在这里查看我的其他答案:stackoverflow.com/questions/21868219/…
  • 能显示UnitCtrl的函数参数和依赖关系吗?
  • @MattWay 是对的,可能是这样。

标签: angularjs ionic


【解决方案1】:

嘿,我刚刚重构了您的代码。试试看

.state('app.units', {
    url: '/units',
    views: {
      'menuContent' :{
        templateUrl: "templates/units.html",
        controller: 'UnitCtrl',
        resolve:{
          units : ['$stateParams','$q','ExamService','$rootScope', '$http',function ($stateParams,$q,ExamService,$rootScope, $http) {
            var deferred = $q.defer();
            $http.get(ApiEndpoint.url +'/units.json?auth_token='+$rootScope.globals.currentUser.userToken)
              .success(function(response){
                //console.log('hello');
                if(response!=undefined)
                  deferred.resolve(response);
                else
                  deffered.reject();
                })
             return deferred.promise;
           }]
        }
      }
    }
  })

【讨论】:

  • 添加延迟是不必要的。
  • 这仍然不起作用。它显示相同的空白结果。奇怪的是没有使用解析,也就是说,在控制器中实现它可以工作并获取所需的数据。我不明白。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-11
  • 2018-06-29
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 2023-02-03
相关资源
最近更新 更多