2017-06-02 34 views
0

我在我的路由.state中解析了我在哪里返回一个列表(来自服务器的数据),它是在console.log中定义的.state路由中定义的,但是在控制器中它未定义console.log它。我正在与离子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
.controller('AppCtrl', ['notificationService', 
       function($ionicModal, $timeout, notificationsResolve, notificationService) { 

你忘3你的4个参数中的哪一个阵列。它应该是

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

或者更好的,你应该只使用基本的语法,没有这个丑陋和容易出错的重复,并使用ng-annotate,使你的代码minifiable你。

+1

正是这...这是问题,多使用插件做这个任务,而不是搞乱它1000倍,被混淆,每次更好。 – shaunhusain

+0

谢谢!两个答案都是正确的,但对于这个建议,我会将其标记为已接受 – Merv

0

PARAMS和他们的名字必须一致

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

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