2017-06-02 4 views
0

Ich habe in meinem Weg .state aufgelöst, wo ich eine Liste (Daten vom Server) zurückgeben, die definiert ist, wenn ich console.log es in der .state Route, aber in Controller ist es nicht definiert, wenn ich console.log es. Ich arbeite mit ionic 1 und AngularJS.

StreckeUndefined Auflösung Eigenschaft in Controller

.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; 
       }); 
    } 
    } 
}) 

-Controller

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

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

    var vm = this; 

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

    vm.notifications = notificationsResolve; 

Antwort

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

Sie haben vergessen, 3 deiner 4 Argumente in der Array. Es sollte

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

Oder besser sein, Sie sollten nur die grundlegende Syntax verwenden, ohne diese hässliche und Bug-anfällig Wiederholung, und verwenden Sie ng-annotate, um Ihren Code minifiable für Sie.

+1

dies genau ... das ist das Problem und viel besser ein Plugin mit dieser Aufgabe zu tun, anstatt es bis 1000-mal durcheinander und jedes Mal, verwirrt zu sein. – shaunhusain

+0

Danke! Beide Antworten sind korrekt, aber für den Vorschlag werde ich dies als akzeptiert markieren – Merv

0

Params und ihre Namen müssen

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

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