angularjs - Calling web service api -
i trying fetch web service api not able display anything, new angularjs please me. have copied controller , factory code using.
controller
app.controller('mycontroller', ['$scope', 'fetchservice', function($scope, fetchservice){ $scope.countries = fetchservice.get(); }]);
service
var app = angular.module('app',[]); app.factory('fetchservice', ['$http', function($http){ return{ get: function(){ return $http.get('api/data4.json').success(function(response){ return response.data; }); } } }]);
the problem fetchservice.get()
asynchronous (a promise returned $http
), have use .then()
so:
app.controller('mycontroller', ['$scope', 'fetchservice', function($scope, fetchservice){ fetchservice.get() .then(function(response) { $scope.countries = response; }); }]);
Comments
Post a Comment