【发布时间】:2015-12-29 02:46:16
【问题描述】:
我试图在我的视图中显示restdata。
我有服务叫"LocationService":
.factory('LocationService', function($resource, $http, $rootScope) {
return {
'getAvailableLocations': function() {
$http.get('/api/v1/locations/available').success(function(response) {
console.log(response); //logs proper response with json
return response;
});
}
}
});
我有一些虚拟控制器:
.controller('DummyCtrl', function($scope, $timeout, $state, $http, $stateParams, LocationService) {
$scope.available_locations = LocationService.getAvailableLocations();
alert($scope.available_locations); // alerts undefined
})
我确信该服务会得到正确的jsonresponse。但是,当我尝试在模板中显示它(或来自控制器的警报)时,我得到了undefined。
我该如何解决这个问题?
【问题讨论】:
-
使用 $resource 而不是 $http。 $resource 将处理承诺。请参阅stackoverflow.com/questions/31455067/… 如何在此处使用 $resource 配置工厂。
标签: rest json json angularjs model-view-controller