AngularJS factory http returns empty -
i'm trying angularjs first time. i'm getting json data http-get request using factory, object returned empty, before ajax-request done.
factory:
mydemo.factory('photosfactory', function($http) { var photos = []; var factory = {}; factory.getphotos = function() { $http({ url: 'content/test_data.json', method: 'get' }).success(function(data, status, headers, config) { photos = data; return photos; }); }; return factory; });
controller:
controllers.appctrl = function($scope, $location, $http, photosfactory) { $scope.photos = []; init(); function init() { $scope.photos = photosfactory.getphotos(); } };
this i've come with. when controller set $scope.photos, value empty if returns photos array before populated ajax response.
you should modify code return promise , use value in controller pls see dummy modified code
mydemo.factory('photosfactory', function($http) { return{ getphotos : function() { return $http({ url: 'content/test_data.json', method: 'get' }) } } });
and controller -
controllers.appctrl = function($scope, $location, $http, photosfactory) { $scope.photos = []; photosfactory.getphotos().success(function(data){ $scope.photos=data; }); };
Comments
Post a Comment