In order to build a Service in angular, first one need to create the service class then define the metadata with a decorator and should import what one need.
The snippet code below is the code for a simple service in angular.
[c]
import { Injectable } from '@angular/core'
@Injectable()
export class ProductService {
getProducts(): IProdcut[] {
}
}
[/c]
In the above code a class is defined and exported so that the service can be used from any other parts of the application. This class currently has one method,
getProducts. This method returns an array of products. Next, add a decorator for the service metadata. When building services, use the
Injectable decorator. This decorator is only really required if the service itself has an injected dependency. However, it is recommended that every service class use the Injectable decorator for clarity and consistency. Lastly, import what one need. In this case,
Injectable.
In the sample application under the products folder create a new file and name it as product.service.ts as shown in the image below.
Now, build a service class by defining the class name and exporting the class, since the service providing products assume the class name as
ProductService as shown in the code below.
product.service.ts
[c]
import { Injectable } from '@angular/core';
import { IProduct } from './product';
@Injectable()
export class ProductService {
getProducts(): IProduct[] {
return [
{
"productId": 1,
"productName": "Angular 2",
"productCode": "SPL-0011",
"releaseDate": "March 19, 2016",
"description": "A Practical Introduction to the new Web Development Platform Angular 2",
"price": 19.95,
"starRating": 3.2,
"imageUrl": "./app/assets/images/angular_2.png"
},
{
"productId": 2,
"productName": "Word Press",
"productCode": "SPL-0023",
"releaseDate": "March 18, 2016",
"description": "This ebook allows you to create a WordPress website on your domainfrom scratch.",
"price": 32.99,
"starRating": 4.2,
"imageUrl": "./app/assets/images/wordpress-logo.png"
},
{
"productId": 5,
"productName": "GitHub",
"productCode": "SPL-0048",
"releaseDate": "May 21, 2016",
"description": "Become a professional coder",
"price": 8.9,
"starRating": 4.8,
"imageUrl": "./app/assets/images/github_logo.png"
},
{
"productId": 8,
"productName": "NodeJs",
"productCode": "SPL-0022",
"releaseDate": "May 15, 2016",
"description": "The content ramps up nicely from basic to advanced.",
"price": 11.55,
"starRating": 3.7,
"imageUrl": "./app/assets/images/nodejs_logo.png"
}
];
}
}
[/c]
Here in the code, the class is decorated with the
injectable decorator and the import statement is added for the injectable decorator, the injected decorator is an optional unless the service has an injected dependency of its own, but it is good practice to add it to every service.
The properties or methods are added to the class as needed. Unless marked private or protected, the properties and methods defined in the class are accessible to any class that uses this service. For the product service, a
getProducts method is needed that returns the list of products. So strongly typed the
return value using the IProduct interface and imported.
Notice that no properties are defined in this class, so this particular service is not used to share data but using it to encapsulate the data access features. By using this service to provide the list of products, one take the responsibility for managing the data away from the individual component. That makes it easier to modify or reuse this logic.
So now done with building a service but in order to use the created service register it with
Angular injector.