How to Create Service in Angular 9 Example

Today, We want to share with you How to Create Service in Angular 9?.In this post we will show you Introduction to services and dependency injection, hear for Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly we will give you demo and example for implement.In this post, we will learn about Angular 9/8 Services & Dependency Injection via providedIn, root & any Tutorial with an example.

How to Create Service in Angular 9?

There are the Following The simple About angular 2 services best practices Full Information With Example and source code.

As I will cover this Post with live Working example to develop angular 8 service example, so the types of services in angular is used for this example is following below.

Create Service

create Post Service:

ng g service Post

src/app/post.service.ts

import { Injectable } from '@angular/core';
  
@Injectable({
  providedIn: 'root'
})
export class PostService {
  
  constructor() { }
  
  getArticles(){
    return [
      {
        id: 1,
        title: 'How To Write PHP Code In Laravel Blade?'
      },
      {
        id: 2,
        title: 'How To Write PHP Code Inside Of Laravel Blade Template?'
      },
      {
        id: 3,
        title: 'How To Check If Array Is Empty In Laravel Blade?'
      },
      {
        id: 4,
        title: 'PHP Laravel Check If Array Is Empty'
      }
    ];
  }
}

Use Service in Component

src/app/app.component.ts

import { Component } from '@angular/core';
import { PostService } from './item.service';
  
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'appNewService';
  articles = [];
  
  constructor(private postService: PostService){
    this.articles = postService.getArticles();
  }
}

View File

src/app/app.component.html

<h1>How to create service in angular 9 using cli - Pakainfo.com</h1>
<ul>
  <li *ngFor="let item of articles">
    <strong>{{ item.id }})</strong>{{ item.title }}
  </li>
</ul>

Web Programming Tutorials Example with Demo

Read :

Read Also:  Different Types of SQL Keys in Database with Example

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about services in angular 6/7/8/9.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.