I'm a little confused how to implement the callback from a successful AJAX call to my RESTful server. This is how I fetch the data:
Service:
app.factory('Data', function($http) {
var factory = {};
factory.get = function(id, success, error) {
$http.post("/api/"+id).success(success);
return factory
};
I prefer having self defined functions (e.g. factory.get()) to use in my controller.
Controller:
app.controller('Ctrl', function($scope, Data) {
var onSuccess = function(data, status) {
// I want to append something to the DOM here
}
};
$scope.get = function(id) {
Data.get(id, onSuccess)
};
});
Here I'm defining a get function I can use in my HTML and I have to pass in an id. However, I have no idea how to access the element so I can append the information from the JSON response to the DOM in the onSuccess function.
Do I have to create a directive to accomplish this? Is this a right way to make AJAX calls?