In this example, we create a factory named myOperation, which contains the Create, ReadList, Read, Update, and Delete methods. The Create method of the myOperation factory requires two parameters: url to the web API and data in order to implement the HTTP post and create a new record in the database. Similarly, the ReadList method will return an array of the data retrieved from the database using HTTP GET. This method needs only one parameter, which is the url to the web API and so on.

This code shows the implementation of a factory in an angularjs controller:

app.controller('ctrlCustomer', function ($scope, $http, myOperations) {

    <!-- USING ANGULAR JS FACTORY -- >
    var Url = '/api/Customer/Customer';

    <! -- GET ALL CUSTOMER -- >
    myOperations.ReadList(Url).success(function (data) {

        $scope.Customers = data;

    }).error(function () {

    });
    <!--GET SPECIFIC CUSTOMER -- >
    $scope.GetCustomerByCustomerID = function (customerID, model) {

        myOperations.Read(Url, { customerID: customerID }).success(function (data) {

            $scope.Customer = data;
            $('#' + model).modal('show');

        }).error(function () {
            //ERROR GOES HERE
        });

    }

    <! -- UPDATE CUSTOMER-- >
    $scope.UpdateCustomer = function (CustomerID, Customer) {

     myOperations.Update(Url, { customerID: CustomerID },    
                         Customer).success(function (data) {

            $scope.Customer = data;

            <! -- GET UPDATED CUSTOMER LIST -- >
            myOperations.ReadList(Url).success(function (data) {

                $scope.Customers = data;

            }).error(function () {

            });



        }).error(function () {


        });


    }
});

In this example, we create the ctrlCustomer controller and inject our myOperation factory into the controller. We used the factory name in order to call the factory method in the controller. For example, myOperations.ReadList(Url).