我想填充選項而不是未定義的值,但似乎在選擇框完成加載後繼續獲取值。
HTML:
<label for="to">Destination:
<select id="to" name="to" ng-model="toSelected" data-ng-options="stop.stop_id as stop.name for stop in stops">
<option value="">Choose a Destination</option>
</select>
</label>
AngularJS廠:
.factory('DataFactory', ['$http', function ($http) {
// Might use a resource here that returns a JSON array
return {
getAllStops: function() {
return $http({
method: 'GET',
url: 'https://myAPIURL.com/api',
headers: {
"Accept": "application/json",
"X-Mashape-Key": "KEYKEYKEYKEYKEYKEYKEYKEYKEYKEYKEY"
}
}).then(function (res) {
return res;
})
},
getAllRoutes: function() {
return someresult similar to the above function;
// more functions.
}
AngularJS控制器:
.controller('MainCtrl', ['$scope', '$http', '_', 'DataFactory', function ($scope, $http, _, DataFactory) {
$scope.fromSelected = "";
$scope.toSelected = "";
DataFactory.getAllStops().then(function successCallback(res) {
console.log(res);
$scope.stops = res.data;
}, function errorCallback(err) {
console.log("error: ");
console.log(err);
});
$scope.changed = function (location, destination) {
console.log($scope.stops);
$scope.possibleRoutes = DataFactory.getPossibleRoutes(location, destination);
「另外,請驗證」stop_id「是否在實際響應中,而不是...... stopID或其他,如果stopID存在,但是您拉動stop.stop_id,它將顯示爲未定義。 關閉。它增加了一個額外的深度,這很奇怪。我必須做res.data.data才能達到我需要的。 –