控制器中未定义的解析 属性

Undefined resolve property in controller

我在我的路线 .state 中解决了我 return 一个列表(来自服务器的数据),当我 console.log 它在 .state 路线中时定义,但在控制器中它是undefined 当我 console.log 它。我正在使用 ionic 1 和 AngularJS。

路线

.state('app', {
  url: '/app',
  abstract: true,
  templateUrl: 'templates/menu.html',
  controller: 'AppCtrl',
  controllerAs: 'appcn',
  resolve: {
    notificationsResolve: function ($http) {
      $http.defaults.headers.common.Authorization = "Basic MzEwMzIzOjEyMw==";
      return $http.get("http://192.168.178.24:8090/notifications/")
              .then(function (response) {
                return response.data;
              });
    }
  }
})

控制器

angular.module('starter.controllers', ['appServiceAPI'])

.controller('AppCtrl', ['notificationService', function($ionicModal, $timeout, notificationsResolve,
                                                        notificationService) {

  var vm = this;

  console.log("resolve: " + notificationsResolve);

  vm.notifications = notificationsResolve;
.controller('AppCtrl', ['notificationService', 
                function($ionicModal, $timeout, notificationsResolve, notificationService) {

您在数组中忘记了 4 个参数中的 3 个。应该是

.controller('AppCtrl', ['$ionicModal', '$timeout', 'notificationsResolve', 'notificationService', 
                function($ionicModal,   $timeout,   notificationsResolve,   notificationService) {

或者更好的是,您应该只使用基本语法,没有这种丑陋且容易出错的重复,并使用 ng-annotate 使您的代码可以缩小。

参数及其名称必须匹配

// this is wrong
.controller('AppCtrl', 
  ['notificationService', 
  function($ionicModal, $timeout, notificationsResolve, notificationService)

// this correct
.controller('AppCtrl', 
  ['$ionicModal', '$timeout', 'notificationsResolve', 'notificationService',
  function($ionicModal, $timeout, notificationsResolve, notificationService)