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

时间:2017-06-02 16:58:04

标签: angularjs controller angular-ui-router undefined resolve

我已经解决了我的路径.state,我返回一个列表(来自服务器的数据),这是我在.state路径中控制它时定义的。但是在控制器中,当我调试它时,它是未定义的。我正在使用离子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;

2 个答案:

答案 0 :(得分:2)

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

你忘记了数组中4个参数中的3个。它应该是

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

或者更好的是,您应该使用基本语法,不要使用这种丑陋且容易出错的重复,并使用ng-annotate使您的代码可以为您量化。

答案 1 :(得分:0)

参数及其名称必须匹配

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

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