Angular 9 Service Example | Create Service in Angular 9
Hello all! In this article, we will talk about how to create service in angular 9. you can see how to create service file in angular 9. let’s discuss about how to create service class in angular 9. if you have question about command to create service in angular 9 then i will give simple example with solution. follow bellow step for create service in angular 9 cli.
There are set of commands provided by angular 9 application. one from there, we will use that command to creating service in angular 9 application.
As we know, service class will help to getting data using api. in angular service we call api and get data from that api. service will easy to available for getting data on angular application. Right now i will give you very simple example without any api, but if you want to know how works service with api then you can follow this tutorial: Angular HttpClient with service example.
Let's see simple example of creating service:
Create Service
Here, we will create simple service using cli command. in service file we will create getPosts() and we will return array.
Let's run bellow command to create Post Service:
ng g service Post
Now you can see there is a created post.service.ts file. you can update like as bellow file:
src/app/post.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class PostService {
constructor() { }
getPosts(){
return [
{
id: 1,
title: 'Angular Http Post Request Example'
},
{
id: 2,
title: 'Angular 9 Routing and Nested Routing Tutorial With Example'
},
{
id: 3,
title: 'How to Create Custom Validators in Angular 9?'
},
{
id: 4,
title: 'How to Create New Component in Angular 9?'
}
];
}
}
Use Service in Component
Here, we will just use that service in our component file and assign to post variable. So let's updated following file:
src/app/app.component.ts
import { Component } from '@angular/core';
import { PostService } from './post.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'appNewService';
posts = [];
constructor(private postService: PostService){
this.posts = postService.getPosts();
}
}
View File
Now we will display our data in view file like as bellow:
src/app/app.component.html
<h1>How to create service in angular 9 using cli - ItSolutionStuff.com</h1>
<ul>
<li *ngFor="let post of posts">
<strong>{{ post.id }})</strong>{{ post.title }}
</li>
</ul>
Now you can run your application by using following command:
ng serve
You can see layout as like bellow screen shot:
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Angular 9 Image Upload Example
- Angular 9 Toastr Notifications Example
- Angular 9 Reactive Form Validation Example
- Angular 9 Form Validation Example
- Angular 9 Font Awesome | Install Font Awesome Icon Angular 9
- Install Bootstrap 4 in Angular 9 | How to Add Bootstrap in Angular 9
- How to Create Component in Angular 9?
- How to Install Angular 9 and Create an Angular 9 Project?