$HTTP Service for loading and posting data

Angular service

Angular $HTTP service is a angular core service wich you can use for communicating with the server or remote resources. If you are working on an angular app you can define you're own angular service as a wrapper around angular $http service. An angular service ojbect is an singleton and ideal for wrapping the angular $http service.

	app.service("resource", function ($http) {
        return {
            getall: function (pageId) {
                return $http.get("/umbraco/Api/Opmerkingenapi/ListOpmerkingen", { params: { id: pageId } });
            },
            save: function (formData) {
                return $http.post("/umbraco/Api/Opmerkingenapi/AddOpmerking", formData);
            }
        };
    });

Angular app

So this is an angular service object with two methods: getall and save. getall takes a parameter and returns a promise with the data. save just saves a form to the server.This service is attached to the angular module app en inserted by denpendency injection so it can be used without creating the object, example:

app.controller('opmerkingencontroller', ['$scope', 'resource', function ($scope, resource) {
   // get alle opmerkingen voor deze pagina
    resource.getall(id).then(function (gevondenOpmerkingen) {
        $scope.opmerkingen = gevondenOpmerkingen.data;
        $scope.HasComments = (gevondenOpmerkingen.data.length > 0 );
    },function (eror) {
        alert('Error tijdens halen data: ' + eror);
    });

Angular promise

The service getall() method doesn't return any data, it returns any promise for the data. This promise cannot be enumerated but has to be executed and the result is in the response. Just like an old-school callback. This is the then funion wich you see in the code example. The angular promise then function is a sort of equivalent of jquery ajax sucess function.

{{opmerking.Naam}}:

{{opmerking.OpmerkingText}}

            

Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!