Services in Angular

 Services


Services are used to encapsulate functionality that is shared across components. Here is an example of a service in Angular:


import { Injectable } from '@angular/core';


@Injectable({

  providedIn: 'root',

})

export class UserService {

  private users = ['Alice', 'Bob', 'Charlie'];


  getUsers() {

    return this.users;

  }


  addUser(name: string) {

    this.users.push(name);

  }

}

This service provides a list of users and methods to get the list and add a new user.

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...