2016-05-24 16 views
0

Ich habe ein Problem beim Laden von JSON-Daten, bevor ich den Hauptcontroller betrete.

Ich verwende this Projekt als Vorlage für mein Projekt. Ich änderte im Grunde nur dist/app/home/home.js und es gibt die veränderten Inhalte:

angular.module('WellJournal', ['uiGmapgoogle-maps', 'ngRoute']) 
    .config(function (uiGmapGoogleMapApiProvider, $routeProvider) { 
     uiGmapGoogleMapApiProvider.configure({ 
      libraries: 'geometry,visualization,places' 
     }); 

     $routeProvider.when('/', { 
      templateUrl: 'index.html', 
      controller: 'MainController', 
      resolve: { 
       markers: ['$http', function ($http) { 
        return $http.get('http://address/api/markers/').then(function (result) { 
         console.log(result.data); 
         return result.data; 
        }); 
       }] 
      } 
     }); 

     $routeProvider.otherwise({ redirectTo: '/' }); 
    }) 
    .controller('MainController', function ($scope, $uibModal) { 
    //trying to access $scope.markers here 
    } 

Und die Sache ist markers: ['$http', function ($http) {...}] nicht ausgelöst werden. Also überprüfte ich die Adresse der Standardseite, die geladen wird (window.location.href) und es stellte sich heraus file:///home/myuser/path/to/project/dir/views/index.html (Sie können den entsprechenden Code here sehen).

Also es ist nicht wirklich einen Server einrichten und öffnet nur lokale Datei (ich denke?). Wie kann ich diese $routeProvider.when(...)-Klausel ausgelöst bekommen? Kann ich das überhaupt machen?

Vielen Dank.

Antwort

0

Wenn jemand anderes das gleiche Problem erlebt - ich nächste Methode am Ende mit:

angular.module('WellJournal', ['uiGmapgoogle-maps']) 
     .run(function($http, $rootScope){ 
      $http.get('http://address/api/markers/'). 
      success(function(data, status, headers, config) { 
       $rootScope.markers = data; 
       $rootScope.$broadcast('markers-loaded'); 
      }). 
      error(function(data, status, headers, config) { 
       // log error 
       alert('error'); 
      }); 
     }) 
     .factory('markers', function ($rootScope) { 
      var markers; 
      $rootScope.$on('markers-loaded', function(){ 
       markers = $rootScope.markers; 
      }); 
      return { 
       data: markers 
      } 
     }) 
     .config(function (uiGmapGoogleMapApiProvider) { 
      uiGmapGoogleMapApiProvider.configure({ 
       libraries: 'weather,geometry,visualization,places' 
      }); 
     }) 
     .controller('MainController', function ($scope, $uibModal, $http, $rootScope, markers) { 
     // use data through markers argument 
     //Warning! This doesn't assure that the data is loaded before page 
     //I'm experiencing small visual lag while loading but this is the best I could do 
     }