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
You can use either resources, or build services that implement http calls.
If you want to use resources, remember:
angular.module('myApp',['ngResource'])
After that you can declare a resource in this fashion:
function MyController($scope, $resource){
var User = $resource('/user/:userId', {userId:'@id'});
var user = User.get({userId:123}, function() {
user.abc = true;
user.$save();
});
}
Alternatively, use services if you need a much deeper level of granularity such as
angular.module('myApp')
.factory('MyAPIService', function($http){
var apiurl, myData;
return {
getData: function(){
$http.get(apiurl)
.success(function(data, status, config, headers){
myData = data;
})
.error(function(){ //handler errors here
});
},
data: function() { return myData; }
};
});
I find services to be great because of the ability to share data between controllers, so you can inject them in a controller like so
myapp.controller('MyController', function($scope, MyAPIService){
$scope.data = MyAPIService.data();
// etc.
});