Creating services with the service

There are other ways to create services with AngularJS. There might be confusion "why should we consider this if we have already used the factory" This decision is all about design. The service is very similar to the factory, however, instead of returning a factory function, it uses a constructor function, which is equivalent to using the new operator. In the following code, we created our parkingService method using a constructor function:
services.js
snippet
parking.service("parkingService", function(parkingConfig) {
    this.calculateTicket = function(car) {
        var departHour = new Date().getHours();
        var entranceHour = car.entrance.getHours();
        var parkingPeriod = departHour– entranceHour;
        var parkingPrice = parkingPeriod * parkingConfig.parkingRate;
        return {
            period: parkingPeriod,
            price: parkingPrice
        };
    };
});
Also, the framework allows us to create services in a more complex and configurable way using the provider function.
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +