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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -