2016-06-21 4 views
0

Ich versuche, die Daten in den Code zu meiner Anwendung zu bringen, hier ist die Fabrik.

angular.module('starter.services', []) 

    .factory('Locations', function ($http, $q) { 
     // Might use a resource here that returns a JSON array 
      var url = "https://portal.mobilequbes.com/api/kiosks?callback=JSON_CALLBACK"; 
      var locations = function() { 
       $http.jsonp(url) 
        .then(function (result) { 
         return result.data; 
        }); 
      }; 

     return { 
      all: function() { 
       return locations; 
      }, 
      get: function (locId) { 
       for (i = 0; i < locations.length;i++){ 
        if (locations[i].friendlyName == parseInt(locId)) { 
         return locations[i]; 
        } 
       } 
      } 
     } 
    }); 

und mein Controller:

.controller('MapCtrl', function ($scope, $http, Locations) { 
    $scope.locations = Locations.all(); 
    $scope.createMarks = function() { 
     createList(Locations.all()); 
    } 
}) 

Wenn es lädt, lädt es einfach nichts oder zwei Objekte, die wie folgt aussehen: ‚‘

Ich bin nicht sicher, warum, weil ich kann nicht scheinen zu erkennen irgendwelche Probleme und ich fühle mich, als hätte ich das zu Tode gelesen. Ich habe die Return-Funktion mit jsFiddle getestet und es hat gut funktioniert, so dass es etwas mit ionic/cordova zu tun hat, bin ich mir ziemlich sicher.

+0

Ihre 'locations'-Funktion wird immer undefiniert zurückgegeben -' return result.data; 'gibt die' .then' Callback-Funktion zurück, nicht 'locations' selbst. –

+0

Wie repariere ich das? – Peter

+0

Tut mir wirklich leid, ich habe keine Zeit, eine vollständige Antwort zu schreiben. Die Antworten auf diese ähnliche Frage sollten Sie jedoch in die richtige Richtung weisen: http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call - der Abschnitt über Versprechen, insbesondere, da dies die API ist, die '$ http' verwendet. –

Antwort

1

In Ihrem Werk tun,

angular.module('starter.services', []) 

.factory('Locations', function ($http, $q) { 
    // Might use a resource here that returns a JSON array 
     var url = "https://portal.mobilequbes.com/api/kiosks?callback=JSON_CALLBACK"; 
     var locations = []; //storing locations for future usage 

    return { 
     all: function() { 
      return $http.jsonp(url) 
       .then(function (result) { 
        locations = result.data; 
        return result.data; 
       }); 
     }, 
     get: function (locId) { 
      for (i = 0; i < locations.length;i++){ 
       if (locations[i].friendlyName == parseInt(locId)) { 
        return locations[i]; 
       } 
      } 
     } 
    } 
}); 

Und in Ihrem Controller

.controller('MapCtrl', function ($scope, $http, Locations) { 
    Locations.all().then(function (locations) { 
     $scope.locations = locations; 
    }); 
    $scope.createMarks = function() { 
     createList(Locations.all()); 
    } 
}) 

Nun ist der Locations.all() -Methode ein Versprechen zurückkehrt, die zu Ergebnis lösen werden .data in Ihrem Controller und Sie können auf Standorte zugreifen.

Bisher haben Sie nichts zurückgegeben, daher war $ scope.locations nicht definiert.

+0

das hat wirklich gut funktioniert, danke! – Peter

+0

MohapatiIch weiß, dass es ziemlich spät ist, aber wenn ich die Liste, die ich aus der URL ziehe, für die Funktion nutzen würde, wie würde ich das tun? Ich habe versucht, die gleiche Methode zu verwenden, aber es gibt nichts zurück. – Peter

+0

Ich habe die Antwort bearbeitet, bitte schauen Sie –

Verwandte Themen