这是调用服务以获取酒店的 angular js 的控制器 vm.getTopHotels = function(){ var hotelsLimit = 10; var top_hotels = dataService.getHotels() .then(function(hotels){ console.log('adf'); sortHotels = commonMethods.sortHotels(hotels.data.data,'Rating','SORT_DESC'); hotelDetailsCheck = checkDetailsIfExists(sortHotels); //Get only top 10 hotels for home page top_hotels = hotelDetailsCheck.slice(0,10); vm.topHotels = top_hotels; }, function(data){ console.log('Failed to get Hotels'); }); }; vm.getTopHotels();** 这是调用 Http get 请求以获取数据的 dataService,但在控制器中,它给了我未定义的数据,因此 datsService 返回方法中是否有问题,因为我认为它没有返回 **(function(){ angular .module('app') .factory('dataService',DataFactory); DataFactory.$inject = ['$http','$q'] function DataFactory($http,$q){ var service = { hotels:[], getHotels:getHotels, saveHotels:saveHotels }; return service; function saveHotels(){ var def = $q.defer(); $http.get('/hotels/saveHotelsData') .then(function successCallback(data){ def.resolve(data); },function errorCallback(data){ def.reject('Something went down :('); }); return def.promise; } function getHotels(){ // var def = $q.defer(); return $http.get('/hotels/getHotelsData') .then(function successCallback(data){ service.hotels = data; }); } }})();
1 回答
largeQ
TA贡献2039条经验 获得超7个赞
// ...
.then(function(data) {
console.log('adf');
sortHotels = commonMethods.sortHotels(hotels.data.data,'Rating','SORT_DESC');
什么hotels?它没有在任何地方声明。如果hotels应该是来自 API 的响应,则应该这样声明:
.then(function(hotels) {
console.log('adf');
sortHotels = commonMethods.sortHotels(hotels.data.data,'Rating','SORT_DESC');
更新:您getHotels通过没有返回语句的函数传递结果,因此将解析为未定义。应该
function getHotels(){
return $http.get('/hotels/getHotelsData')
.then(function successCallback(data) {
service.hotels = data;
return data;
});
}
添加回答
举报
0/150
提交
取消