From where should REST API request be invoked from angular js?
I.e. from controller, module, service, factory. I am totally confused that what should be the right w
This is how we do this, Write the http
service as a factory
method.
Edited as per comments to use Promise API
var MyApp = angular.module('App', []);
MyApp .factory('ApiFactory', ['$http',
function ($http) {
return {
getUsers: function (rowcount) {
var promise = $http.get('api/Users/' + rowcount) .then(function(response) {
return response.data;
}, function (error) {
//error
})
return promise;
}
}
}
]);
Now you can use it in controller
as this.
MyApp .controller('MyController', ['$scope', 'ApiFactory',
function ($scope, ApiFactory) {
$scope.Users = null;
ApiFactory.getUsers(count).then(function(data)
{
$scope.Users = data;
});
}
]);