json - How to use HTTP.GET in AngularJS correctly? (Dynamically) -
i create has service, , pass controller, problem have read static file (1.json), , have populated folder more 1 json, know, how can bring them in, , make call dynamically.
service:
todoapp.factory('eventdata', function($http, $q){ return { getevent: function(){ var deferred = $q.defer(); $http({method: 'get', url: '/data/phonebook/1'}). success(function (data, status, headers, config){ deferred.resolve(data); }). error(function (data, status, headers, config){ deferred.reject(status); }); return deferred.promise; } }; });
controller:
todoapp.controller('feedercontroller', function feedercontroller($scope, eventdata) { eventdata.getevent().then( function(event){$scope.event = event;}, function(statuscode) {console.log(statuscode)}); } );
best wishes
you'll want parameterize service call. once there can change code handle 1=>n calls rather 1 using loop.
todoapp.factory('eventdata', function($http, $q){ return { getevent: function(id){ var deferred = $q.defer(); $http({method: 'get', url: '/data/phonebook/'+id}). success(function (data, status, headers, config){ deferred.resolve(data); }). error(function (data, status, headers, config){ deferred.reject(status); }); return deferred.promise; } }; });
and controller becomes
todoapp.controller('feedercontroller', function feedercontroller($scope, eventdata) { $scope.events = []; for(var i=0; i<10; i++){ eventdata.getevent(i).then( function(event){$scope.events.push(event);}, function(statuscode) {console.log(statuscode)}); } } );
Comments
Post a Comment