Skip to main content

Command Palette

Search for a command to run...

Activity 23: Research Angular Services

Published
•19 min read•View as Markdown

Angular Services Meaning

In Angular, a service is a class that provides a specific functionality or data to other components, directives, or services. Services are a fundamental concept in Angular’s dependency injection system, enabling components to decouple and reuse code.

Key Characteristics of Angular Services:

  1. Singleton: Services are instantiated only once, and their instances are shared across the entire application.

  2. Reusable: Services can be used by multiple components, directives, or services, promoting code reuse and reducing duplication.

  3. Decoupled: Services provide a layer of abstraction, allowing components to interact with them without knowing the implementation details.

  4. Injectable: Services are registered with the Angular injector, making them available for injection into components, directives, or other services.

Common Use Cases for Angular Services:

  1. Data Storage: Services can store and manage data, such as caching, filtering, or formatting, and provide it to components as needed.

  2. Business Logic: Services can encapsulate complex business logic, calculations, or validations, and expose them to components through interfaces.

  3. API Communication: Services can handle communication with external APIs, such as making HTTP requests or handling WebSocket connections.

  4. Utility Functions: Services can provide utility functions, like string formatting, date manipulation, or mathematical calculations, to components.

Benefits of Using Angular Services:

  1. Modularity: Services promote modular code, making it easier to maintain and update individual components without affecting others.

  2. Reusability: Services enable code reuse across the application, reducing duplication and improving overall efficiency.

  3. Testability: Services can be easily tested in isolation, making it simpler to write unit tests and ensure correct functionality.

  4. Flexibility: Services provide a flexible way to adapt to changing requirements or new features, as they can be updated or replaced without affecting other parts of the application.

Example:

  1. Student list

Service

import { Injectable } from '@angular/core';
import { Student } from '../types';

@Injectable({
  providedIn: 'root',
})
export class StudentService {
  private studentList: Student[] = [
    {
      name: 'Mike',
      year: '4th',
      section: 'Luna',
    },
  ];

  getStudentList() {
    return this.studentList;
  }

  addStudent(student: Student): void {
    this.studentList.push(student);
  }

  deleteStudent(index: number): void {
    this.studentList.splice(index, 1);
  }

  editStudent(index: number, newStudent: Student) {
    this.studentList[index] = newStudent;
  }
}

Output:

  1. Employee list

Services

// employee.service.ts
import { Injectable } from '@angular/core';
import { Employee } from '../types';

@Injectable({
  providedIn: 'root',
})
export class EmployeeService {
  private employeeList: Employee[] = [
    {
      name: 'John Doe',
      work: 'Developer',
    },
  ];

  getEmployeeList() {
    return this.employeeList;
  }

  addEmployee(employee: Employee): void {
    this.employeeList.push(employee);
  }

  deleteEmployee(index: number): void {
    this.employeeList.splice(index, 1);
  }

  editEmployee(index: number, newEmployee: Employee) {
    this.employeeList[index] = newEmployee;
  }
}

Output:

  1. Fruit list

Services

import { Injectable } from '@angular/core';
import { Fruit } from '../types';

@Injectable({
  providedIn: 'root',
})
export class FruitService {
  private fruitList: Fruit[] = [
    {
      name: 'Apple',
    },
    {
      name: 'Banana',
    },
  ];

  getFruitList() {
    return this.fruitList;
  }

  addFruit(fruit: Fruit): void {
    this.fruitList.push(fruit);
  }

  deleteFruit(index: number): void {
    this.fruitList.splice(index, 1);
  }

  editFruit(index: number, newFruit: Fruit) {
    this.fruitList[index] = newFruit;
  }
}

Output:

  1. Course list

Services

import { Injectable } from '@angular/core';
import { Course } from '../types';

@Injectable({
  providedIn: 'root',
})
export class CourseService {
  private courseList: Course[] = [
    {
      code: 'CS101',
      name: 'Computer Science Basics',
    },
  ];

  getCourseList() {
    return this.courseList;
  }

  addCourse(course: Course): void {
    this.courseList.push(course);
  }

  deleteCourse(index: number): void {
    this.courseList.splice(index, 1);
  }

  editCourse(index: number, newCourse: Course) {
    this.courseList[index] = newCourse;
  }
}

Output:

  1. Book list

Services

import { Injectable } from '@angular/core';
import { Book } from '../types';

@Injectable({
  providedIn: 'root',
})
export class BookService {
  private bookList: Book[] = [
    {
      name: 'The Great Gatsby',
      publishDate: '1925-04-10',
      author: 'F. Scott Fitzgerald',
    },
    {
      name: '1984',
      publishDate: '1949-06-08',
      author: 'George Orwell',
    },
  ];

  getBookList() {
    return this.bookList;
  }

  addBook(book: Book): void {
    this.bookList.push(book);
  }

  deleteBook(index: number): void {
    this.bookList.splice(index, 1);
  }

  editBook(index: number, newBook: Book) {
    this.bookList[index] = newBook;
  }
}

Output:

  1. City list

Services

// city.service.ts
import { Injectable } from '@angular/core';
import { City } from '../types';

@Injectable({
  providedIn: 'root',
})
export class CityService {
  private cityList: City[] = [
    { city: 'New York' },
    { city: 'Los Angeles' },
    { city: 'Chicago' },
  ];

  getCityList() {
    return this.cityList;
  }

  addCity(city: City): void {
    this.cityList.push(city);
  }

  deleteCity(index: number): void {
    this.cityList.splice(index, 1);
  }

  editCity(index: number, newCity: City) {
    this.cityList[index] = newCity;
  }
}

Output:

  1. Movie list

Services

import { Injectable } from '@angular/core';
import { Movie } from '../types';

@Injectable({
  providedIn: 'root',
})
export class MovieService {
  private movieList: Movie[] = [
    { name: 'Inception', rating: 8.8 },
    { name: 'Interstellar', rating: 8.6 },
    { name: 'The Dark Knight', rating: 9.0 },
  ];

  getMovieList() {
    return this.movieList;
  }

  addMovie(movie: Movie): void {
    this.movieList.push(movie);
  }

  deleteMovie(index: number): void {
    this.movieList.splice(index, 1);
  }

  editMovie(index: number, newMovie: Movie) {
    this.movieList[index] = newMovie;
  }
}

Output:

  1. Car model list

Services

// car-model.service.ts
import { Injectable } from '@angular/core';
import { CarModel } from '../types';

@Injectable({
  providedIn: 'root',
})
export class CarModelService {
  private carModelList: CarModel[] = [{ name: 'Ford Mustang', year: 2021 }];

  getCarModelList() {
    return this.carModelList;
  }

  addCarModel(carModel: CarModel): void {
    this.carModelList.push(carModel);
  }

  deleteCarModel(index: number): void {
    this.carModelList.splice(index, 1);
  }

  editCarModel(index: number, newCarModel: CarModel) {
    this.carModelList[index] = newCarModel;
  }
}

Output:

  1. Product list

Services

import { Injectable } from '@angular/core';
import { Product } from '../types';

@Injectable({
  providedIn: 'root',
})
export class ProductService {
  private productList: Product[] = [
    { name: 'Laptop', price: 999 },
    { name: 'Smartphone', price: 499 },
    { name: 'Tablet', price: 299 },
  ];

  getProductList() {
    return this.productList;
  }

  addProduct(product: Product): void {
    this.productList.push(product);
  }

  deleteProduct(index: number): void {
    this.productList.splice(index, 1);
  }

  editProduct(index: number, newProduct: Product) {
    this.productList[index] = newProduct;
  }
}

Output:

  1. Subject list

Services

import { Injectable } from '@angular/core';
import { Subject } from '../types';

@Injectable({
  providedIn: 'root',
})
export class SubjectService {
  private subjectList: Subject[] = [
    { name: 'Mathematics', time: '9:00 AM' },
    { name: 'Science', time: '10:30 AM' },
    { name: 'History', time: '12:00 PM' },
  ];

  getSubjectList() {
    return this.subjectList;
  }

  addSubject(subject: Subject): void {
    this.subjectList.push(subject);
  }

  deleteSubject(index: number): void {
    this.subjectList.splice(index, 1);
  }

  editSubject(index: number, newSubject: Subject) {
    this.subjectList[index] = newSubject;
  }
}

Output:

  1. Country list

Services

// country.service.ts
import { Injectable } from '@angular/core';
import { Country } from '../types';

@Injectable({
  providedIn: 'root',
})
export class CountryService {
  private countryList: Country[] = [
    { name: 'USA' },
    { name: 'Canada' },
    { name: 'Japan' },
  ];

  getCountryList() {
    return this.countryList;
  }

  addCountry(country: Country): void {
    this.countryList.push(country);
  }

  deleteCountry(index: number): void {
    this.countryList.splice(index, 1);
  }

  editCountry(index: number, newCountry: Country) {
    this.countryList[index] = newCountry;
  }
}

Output:

  1. Sports list

Services

import { Injectable } from '@angular/core';
import { Sport } from '../types';

@Injectable({
  providedIn: 'root',
})
export class SportService {
  private sportList: Sport[] = [
    {
      name: 'Basketball',
    },
  ];

  getSportList() {
    return this.sportList;
  }

  addSport(sport: Sport): void {
    this.sportList.push(sport);
  }

  deleteSport(index: number): void {
    this.sportList.splice(index, 1);
  }

  editSport(index: number, newSport: Sport) {
    this.sportList[index] = newSport;
  }
}

Output:

  1. Vegetable list

Services

import { Injectable } from '@angular/core';
import { Vegetable } from '../types';

@Injectable({
  providedIn: 'root',
})
export class VegetableService {
  private vegetableList: Vegetable[] = [
    {
      name: 'Carrot',
    },
  ];

  getVegetableList() {
    return this.vegetableList;
  }

  addVegetable(vegetable: Vegetable): void {
    this.vegetableList.push(vegetable);
  }

  deleteVegetable(index: number): void {
    this.vegetableList.splice(index, 1);
  }

  editVegetable(index: number, newVegetable: Vegetable) {
    this.vegetableList[index] = newVegetable;
  }
}

Output:

  1. Animal list

Services

import { Injectable } from '@angular/core';
import { Animal } from '../types';

@Injectable({
  providedIn: 'root',
})
export class AnimalService {
  private animalList: Animal[] = [
    {
      name: 'Dog',
    },
  ];

  getAnimalList() {
    return this.animalList;
  }

  addAnimal(animal: Animal): void {
    this.animalList.push(animal);
  }

  deleteAnimal(index: number): void {
    this.animalList.splice(index, 1);
  }

  editAnimal(index: number, newAnimal: Animal) {
    this.animalList[index] = newAnimal;
  }
}

Output:

  1. Tools list

Services

import { Injectable } from '@angular/core';
import { Tool } from '../types';

@Injectable({
  providedIn: 'root',
})
export class ToolService {
  private toolList: Tool[] = [
    {
      name: 'Hammer',
      price: 25,
    },
  ];

  getToolList() {
    return this.toolList;
  }

  addTool(tool: Tool): void {
    this.toolList.push(tool);
  }

  deleteTool(index: number): void {
    this.toolList.splice(index, 1);
  }

  editTool(index: number, newTool: Tool) {
    this.toolList[index] = newTool;
  }
}

Output:

  1. Language list

Services

import { Injectable } from '@angular/core';
import { Language } from '../types';

@Injectable({
  providedIn: 'root',
})
export class LanguageService {
  private languageList: Language[] = [
    {
      language: 'English',
    },
    {
      language: 'Spanish',
    },
  ];

  getLanguageList() {
    return this.languageList;
  }

  addLanguage(language: Language): void {
    this.languageList.push(language);
  }

  deleteLanguage(index: number): void {
    this.languageList.splice(index, 1);
  }

  editLanguage(index: number, newLanguage: Language) {
    this.languageList[index] = newLanguage;
  }
}

Output:

  1. Game list

Services

import { Injectable } from '@angular/core';
import { Game } from '../types';

@Injectable({
  providedIn: 'root',
})
export class GameService {
  private gameList: Game[] = [
    {
      name: 'Chess',
      year: 1950,
    },
    {
      name: 'Monopoly',
      year: 1935,
    },
  ];

  getGameList() {
    return this.gameList;
  }

  addGame(game: Game): void {
    this.gameList.push(game);
  }

  deleteGame(index: number): void {
    this.gameList.splice(index, 1);
  }

  editGame(index: number, newGame: Game) {
    this.gameList[index] = newGame;
  }
}

Output:

  1. Software list

Services

import { Injectable } from '@angular/core';
import { Software } from '../types';

@Injectable({
  providedIn: 'root',
})
export class SoftwareService {
  private softwareList: Software[] = [
    {
      name: 'Photoshop',
    },
    {
      name: 'VS Code',
    },
  ];

  getSoftwareList() {
    return this.softwareList;
  }

  addSoftware(software: Software): void {
    this.softwareList.push(software);
  }

  deleteSoftware(index: number): void {
    this.softwareList.splice(index, 1);
  }

  editSoftware(index: number, newSoftware: Software) {
    this.softwareList[index] = newSoftware;
  }
}

Output:

  1. Phone contact list

Services

import { Injectable } from '@angular/core';
import { Contact } from '../types';

@Injectable({
  providedIn: 'root',
})
export class ContactService {
  private contactList: Contact[] = [
    {
      name: 'John Doe',
      phoneNumber: '091212727112',
    },
  ];

  getContactList() {
    return this.contactList;
  }

  addContact(contact: Contact): void {
    this.contactList.push(contact);
  }

  deleteContact(index: number): void {
    this.contactList.splice(index, 1);
  }

  editContact(index: number, newContact: Contact) {
    this.contactList[index] = newContact;
  }
}

Output:

  1. Music list

Services

import { Injectable } from '@angular/core';
import { Music } from '../types';

@Injectable({
  providedIn: 'root',
})
export class MusicService {
  private musicList: Music[] = [
    {
      title: 'Shape of You',
      artist: 'Ed Sheeran',
    },
    {
      title: 'Blinding Lights',
      artist: 'The Weeknd',
    },
  ];

  getMusicList() {
    return this.musicList;
  }

  addMusic(music: Music): void {
    this.musicList.push(music);
  }

  deleteMusic(index: number): void {
    this.musicList.splice(index, 1);
  }

  editMusic(index: number, newMusic: Music) {
    this.musicList[index] = newMusic;
  }
}

Output:

  1. Food menu list

Services

import { Injectable } from '@angular/core';
import { Food } from '../types';

@Injectable({
  providedIn: 'root',
})
export class FoodService {
  private foodList: Food[] = [
    {
      name: 'Pizza',
      price: 10.99,
    },
    {
      name: 'Burger',
      price: 5.99,
    },
  ];

  getFoodList() {
    return this.foodList;
  }

  addFood(food: Food): void {
    this.foodList.push(food);
  }

  deleteFood(index: number): void {
    this.foodList.splice(index, 1);
  }

  editFood(index: number, newFood: Food) {
    this.foodList[index] = newFood;
  }
}

Output:

  1. Grocery list

Services

import { Injectable } from '@angular/core';
import { Grocery } from '../types';

@Injectable({
  providedIn: 'root',
})
export class GroceryService {
  private groceryList: Grocery[] = [
    {
      name: 'Rice',
      price: 1.99,
    },
    {
      name: 'Bread',
      price: 2.49,
    },
  ];

  getGroceryList() {
    return this.groceryList;
  }

  addGrocery(grocery: Grocery): void {
    this.groceryList.push(grocery);
  }

  deleteGrocery(index: number): void {
    this.groceryList.splice(index, 1);
  }

  editGrocery(index: number, newGrocery: Grocery) {
    this.groceryList[index] = newGrocery;
  }
}

Output:

  1. Classroom list

Services

import { Injectable } from '@angular/core';
import { Classroom } from '../types';

@Injectable({
  providedIn: 'root',
})
export class ClassroomService {
  private classroomList: Classroom[] = [
    {
      name: 'Math 101',
      roomNumber: 101,
    },
    {
      name: 'Science 102',
      roomNumber: 102,
    },
  ];

  getClassroomList() {
    return this.classroomList;
  }

  addClassroom(classroom: Classroom): void {
    this.classroomList.push(classroom);
  }

  deleteClassroom(index: number): void {
    this.classroomList.splice(index, 1);
  }

  editClassroom(index: number, newClassroom: Classroom) {
    this.classroomList[index] = newClassroom;
  }
}

Output:

  1. Inventory list

Services

import { Injectable } from '@angular/core';
import { Inventory } from '../types';

@Injectable({
  providedIn: 'root',
})
export class InventoryService {
  private inventoryList: Inventory[] = [
    {
      name: 'Pencil',
      quantity: 100,
    },
    {
      name: 'Notebook',
      quantity: 50,
    },
  ];

  getInventoryList() {
    return this.inventoryList;
  }

  addInventoryItem(item: Inventory): void {
    this.inventoryList.push(item);
  }

  deleteInventoryItem(index: number): void {
    this.inventoryList.splice(index, 1);
  }

  editInventoryItem(index: number, newItem: Inventory) {
    this.inventoryList[index] = newItem;
  }
}

Output:

  1. Lecture list

Services

import { Injectable } from '@angular/core';
import { Lecture } from '../types';

@Injectable({
  providedIn: 'root',
})
export class LectureService {
  private lectureList: Lecture[] = [
    {
      name: 'Introduction to Programming',
      course: 'CS101',
    },
    {
      name: 'Data Structures',
      course: 'CS102',
    },
  ];

  getLectureList() {
    return this.lectureList;
  }

  addLecture(lecture: Lecture): void {
    this.lectureList.push(lecture);
  }

  deleteLecture(index: number): void {
    this.lectureList.splice(index, 1);
  }

  editLecture(index: number, newLecture: Lecture) {
    this.lectureList[index] = newLecture;
  }
}

Output:

  1. Stationery list

Services

import { Injectable } from '@angular/core';
import { Stationery } from '../types';

@Injectable({
  providedIn: 'root',
})
export class StationeryService {
  private stationeryList: Stationery[] = [
    {
      name: 'Pencil',
    },
    {
      name: 'Notebook',
    },
  ];

  getStationeryList() {
    return this.stationeryList;
  }

  addStationery(stationery: Stationery): void {
    this.stationeryList.push(stationery);
  }

  deleteStationery(index: number): void {
    this.stationeryList.splice(index, 1);
  }

  editStationery(index: number, newStationery: Stationery) {
    this.stationeryList[index] = newStationery;
  }
}

Output:

  1. Flower list

Services

import { Injectable } from '@angular/core';
import { Flower } from '../types';

@Injectable({
  providedIn: 'root',
})
export class FlowerService {
  private flowerList: Flower[] = [
    {
      name: 'Rose',
    },
    {
      name: 'Tulip',
    },
  ];

  getFlowerList() {
    return this.flowerList;
  }

  addFlower(flower: Flower): void {
    this.flowerList.push(flower);
  }

  deleteFlower(index: number): void {
    this.flowerList.splice(index, 1);
  }

  editFlower(index: number, newFlower: Flower) {
    this.flowerList[index] = newFlower;
  }
}

Output:

  1. Destination list

Services

      import { Injectable } from '@angular/core';
  import { Destination } from '../types';

  @Injectable({
    providedIn: 'root',
  })
  export class DestinationService {
    private destinationList: Destination[] = [
      {
        place: 'Paris',
        country: 'France',
      },
      {
        place: 'Tokyo',
        country: 'Japan',
      },
    ];

    getDestinationList() {
      return this.destinationList;
    }

    addDestination(destination: Destination): void {
      this.destinationList.push(destination);
    }

    deleteDestination(index: number): void {
      this.destinationList.splice(index, 1);
    }

    editDestination(index: number, newDestination: Destination) {
      this.destinationList[index] = newDestination;
    }
  }

Output:

  1. Laptop list

Service

import { Injectable } from '@angular/core';
import { Laptop } from '../types';

@Injectable({
  providedIn: 'root',
})
export class LaptopService {
  private laptopList: Laptop[] = [
    {
      model: 'Dell',
      price: 999,
    },
    {
      model: 'MacBook',
      price: 1299,
    },
  ];

  getLaptopList() {
    return this.laptopList;
  }

  addLaptop(laptop: Laptop): void {
    this.laptopList.push(laptop);
  }

  deleteLaptop(index: number): void {
    this.laptopList.splice(index, 1);
  }

  editLaptop(index: number, newLaptop: Laptop) {
    this.laptopList[index] = newLaptop;
  }
}

Output:

  1. Laptop specification list

Service

import { Injectable } from '@angular/core';
import { LaptopSpecification } from '../types';

@Injectable({
  providedIn: 'root',
})
export class LaptopSpecificationService {
  private specificationList: LaptopSpecification[] = [
    {
      specs: 'Intel i7, 16GB RAM, 512GB SSD',
    },
    {
      specs: 'AMD Ryzen 5, 8GB RAM, 256GB SSD',
    },
  ];

  getSpecificationList() {
    return this.specificationList;
  }

  addSpecification(specification: LaptopSpecification): void {
    this.specificationList.push(specification);
  }

  deleteSpecification(index: number): void {
    this.specificationList.splice(index, 1);
  }

  editSpecification(index: number, newSpecification: LaptopSpecification) {
    this.specificationList[index] = newSpecification;
  }
}

Output:

  1. Computer hardware list

Service

import { Injectable } from '@angular/core';
import { ComputerHardware } from '../types';

@Injectable({
  providedIn: 'root',
})
export class ComputerHardwareService {
  private hardwareList: ComputerHardware[] = [
    {
      name: 'Graphics Card',
    },
    {
      name: 'Motherboard',
    },
  ];

  getHardwareList() {
    return this.hardwareList;
  }

  addHardware(hardware: ComputerHardware): void {
    this.hardwareList.push(hardware);
  }

  deleteHardware(index: number): void {
    this.hardwareList.splice(index, 1);
  }

  editHardware(index: number, newHardware: ComputerHardware) {
    this.hardwareList[index] = newHardware;
  }
}

Output:

  1. Mobile app list

Service

import { Injectable } from '@angular/core';
import { MobileApp } from '../types';

@Injectable({
  providedIn: 'root',
})
export class MobileAppService {
  private mobileAppList: MobileApp[] = [
    {
      name: 'Facebook',
      platform: 'iOS',
    },
    {
      name: 'WhatsApp',
      platform: 'Android',
    },
  ];

  getMobileAppList() {
    return this.mobileAppList;
  }

  addMobileApp(app: MobileApp): void {
    this.mobileAppList.push(app);
  }

  deleteMobileApp(index: number): void {
    this.mobileAppList.splice(index, 1);
  }

  editMobileApp(index: number, newApp: MobileApp) {
    this.mobileAppList[index] = newApp;
  }
}

Output:

  1. Video list

Service

import { Injectable } from '@angular/core';
import { Video } from '../types';

@Injectable({
  providedIn: 'root',
})
export class VideoService {
  private videoList: Video[] = [
    {
      title: 'Introduction to Angular',
      views: 1500,
    },
    {
      title: 'Learn TypeScript in 30 Minutes',
      views: 800,
    },
  ];

  getVideoList() {
    return this.videoList;
  }

  addVideo(video: Video): void {
    this.videoList.push(video);
  }

  deleteVideo(index: number): void {
    this.videoList.splice(index, 1);
  }

  editVideo(index: number, newVideo: Video) {
    this.videoList[index] = newVideo;
  }
}

Output:

  1. TV Show list

Service

import { Injectable } from '@angular/core';
import { TVShow } from '../types';

@Injectable({
  providedIn: 'root',
})
export class TVShowService {
  private tvShowList: TVShow[] = [
    {
      title: 'Breaking Bad',
      time: '9:00 PM',
    },
    {
      title: 'Game of Thrones',
      time: '8:00 PM',
    },
  ];

  getTVShowList() {
    return this.tvShowList;
  }

  addTVShow(show: TVShow): void {
    this.tvShowList.push(show);
  }

  deleteTVShow(index: number): void {
    this.tvShowList.splice(index, 1);
  }

  editTVShow(index: number, newShow: TVShow) {
    this.tvShowList[index] = newShow;
  }
}

Output:

  1. Furniture list

Services

import { Injectable } from '@angular/core';
import { Furniture } from '../types';

@Injectable({
  providedIn: 'root',
})
export class FurnitureService {
  private furnitureList: Furniture[] = [
    {
      name: 'Sofa',
      price: 300,
    },
    {
      name: 'Dining Table',
      price: 150,
    },
  ];

  getFurnitureList() {
    return this.furnitureList;
  }

  addFurniture(furniture: Furniture): void {
    this.furnitureList.push(furniture);
  }

  deleteFurniture(index: number): void {
    this.furnitureList.splice(index, 1);
  }

  editFurniture(index: number, newFurniture: Furniture) {
    this.furnitureList[index] = newFurniture;
  }
}

Output:

  1. Phone accessory list

Service

import { Injectable } from '@angular/core';
import { PhoneAccessory } from '../types';

@Injectable({
  providedIn: 'root',
})
export class PhoneAccessoryService {
  private phoneAccessoryList: PhoneAccessory[] = [
    {
      name: 'Phone Case',
    },
    {
      name: 'Screen Protector',
    },
  ];

  getPhoneAccessoryList() {
    return this.phoneAccessoryList;
  }

  addPhoneAccessory(accessory: PhoneAccessory): void {
    this.phoneAccessoryList.push(accessory);
  }

  deletePhoneAccessory(index: number): void {
    this.phoneAccessoryList.splice(index, 1);
  }

  editPhoneAccessory(index: number, newAccessory: PhoneAccessory) {
    this.phoneAccessoryList[index] = newAccessory;
  }
}

Output:

  1. Building list

Service

import { Injectable } from '@angular/core';
import { Building } from '../types';

@Injectable({
  providedIn: 'root',
})
export class BuildingService {
  private buildingList: Building[] = [
    {
      name: 'Library',
    },
    {
      name: 'Gymnasium',
    },
  ];

  getBuildingList() {
    return this.buildingList;
  }

  addBuilding(building: Building): void {
    this.buildingList.push(building);
  }

  deleteBuilding(index: number): void {
    this.buildingList.splice(index, 1);
  }

  editBuilding(index: number, newBuilding: Building) {
    this.buildingList[index] = newBuilding;
  }
}

Output:

  1. Painting list

Service

import { Injectable } from '@angular/core';
import { Painting } from '../types';

@Injectable({
  providedIn: 'root',
})
export class PaintingService {
  private paintingList: Painting[] = [
    {
      title: 'Starry Night',
      year: 1889,
    },
    {
      title: 'Mona Lisa',
      year: 1503,
    },
  ];

  getPaintingList() {
    return this.paintingList;
  }

  addPainting(painting: Painting): void {
    this.paintingList.push(painting);
  }

  deletePainting(index: number): void {
    this.paintingList.splice(index, 1);
  }

  editPainting(index: number, newPainting: Painting) {
    this.paintingList[index] = newPainting;
  }
}

Output:

  1. Artist list

Service

import { Injectable } from '@angular/core';
import { Artist } from '../types';

@Injectable({
  providedIn: 'root',
})
export class ArtistService {
  private artistList: Artist[] = [
    {
      name: 'Vincent van Gogh',
    },
    {
      name: 'Pablo Picasso',
    },
  ];

  getArtistList() {
    return this.artistList;
  }

  addArtist(artist: Artist): void {
    this.artistList.push(artist);
  }

  deleteArtist(index: number): void {
    this.artistList.splice(index, 1);
  }

  editArtist(index: number, newArtist: Artist) {
    this.artistList[index] = newArtist;
  }
}

Output:

  1. Composer list

Service

import { Injectable } from '@angular/core';
import { Composer } from '../types';

@Injectable({
  providedIn: 'root',
})
export class ComposerService {
  private composerList: Composer[] = [
    {
      name: 'Ludwig van Beethoven',
    },
    {
      name: 'Johann Sebastian Bach',
    },
  ];

  getComposerList() {
    return this.composerList;
  }

  addComposer(composer: Composer): void {
    this.composerList.push(composer);
  }

  deleteComposer(index: number): void {
    this.composerList.splice(index, 1);
  }

  editComposer(index: number, newComposer: Composer) {
    this.composerList[index] = newComposer;
  }
}

Output:

  1. Podcast list

Service

import { Injectable } from '@angular/core';
import { Podcast } from '../types';

@Injectable({
  providedIn: 'root',
})
export class PodcastService {
  private podcastList: Podcast[] = [
    {
      title: 'Tech Talk',
      speaker: 'John Doe',
    },
    {
      title: 'History Deep Dive',
      speaker: 'Jane Smith',
    },
  ];

  getPodcastList(): Podcast[] {
    return this.podcastList;
  }

  addPodcast(podcast: Podcast): void {
    this.podcastList.push(podcast);
  }

  deletePodcast(index: number): void {
    this.podcastList.splice(index, 1);
  }

  editPodcast(index: number, newPodcast: Podcast): void {
    this.podcastList[index] = newPodcast;
  }
}

Output:

  1. Exercise list

Service

import { Injectable } from '@angular/core';
import { Exercise } from '../types';

@Injectable({
  providedIn: 'root',
})
export class ExerciseService {
  private exerciseList: Exercise[] = [
    {
      name: 'Push-Ups',
      repetitions: 20,
    },
    {
      name: 'Sit-Ups',
      repetitions: 30,
    },
  ];

  getExerciseList(): Exercise[] {
    return this.exerciseList;
  }

  addExercise(exercise: Exercise): void {
    this.exerciseList.push(exercise);
  }

  deleteExercise(index: number): void {
    this.exerciseList.splice(index, 1);
  }

  editExercise(index: number, newExercise: Exercise): void {
    this.exerciseList[index] = newExercise;
  }
}

Output:

  1. Meal plan list

Service

import { Injectable } from '@angular/core';
import { Meal } from '../types';

@Injectable({
  providedIn: 'root',
})
export class MealService {
  private mealList: Meal[] = [
    {
      name: 'Breakfast',
    },
    {
      name: 'Lunch',
    },
  ];

  getMealList(): Meal[] {
    return this.mealList;
  }

  addMeal(meal: Meal): void {
    this.mealList.push(meal);
  }

  deleteMeal(index: number): void {
    this.mealList.splice(index, 1);
  }

  editMeal(index: number, newMeal: Meal): void {
    this.mealList[index] = newMeal;
  }
}

Output:

  1. Budget list

Service

import { Injectable } from '@angular/core';
import { Budget } from '../types';

@Injectable({
  providedIn: 'root',
})
export class BudgetService {
  private budgetList: Budget[] = [
    {
      projectName: 'Website Development',
      budget: 5000,
    },
    {
      projectName: 'Marketing Campaign',
      budget: 3000,
    },
  ];

  getBudgetList(): Budget[] {
    return this.budgetList;
  }

  addBudget(budget: Budget): void {
    this.budgetList.push(budget);
  }

  deleteBudget(index: number): void {
    this.budgetList.splice(index, 1);
  }

  editBudget(index: number, newBudget: Budget): void {
    this.budgetList[index] = newBudget;
  }
}

Output:

  1. Presentation list

Service

import { Injectable } from '@angular/core';
import { Presentation } from '../types';

@Injectable({
  providedIn: 'root',
})
export class PresentationService {
  private presentationList: Presentation[] = [
    {
      topicTitle: 'Introduction to Angular',
    },
    {
      topicTitle: 'Advanced TypeScript',
    },
  ];

  getPresentationList(): Presentation[] {
    return this.presentationList;
  }

  addPresentation(presentation: Presentation): void {
    this.presentationList.push(presentation);
  }

  deletePresentation(index: number): void {
    this.presentationList.splice(index, 1);
  }

  editPresentation(index: number, newPresentation: Presentation): void {
    this.presentationList[index] = newPresentation;
  }
}

Output:

  1. Tour list

Services

import { Injectable } from '@angular/core';
import { Tour } from '../types';

@Injectable({
  providedIn: 'root',
})
export class TourService {
  private tourList: Tour[] = [
    {
      place: 'Paris',
      date: '2024-06-12',
    },
    {
      place: 'Tokyo',
      date: '2024-09-15',
    },
  ];

  getTourList(): Tour[] {
    return this.tourList;
  }

  addTour(tour: Tour): void {
    this.tourList.push(tour);
  }

  deleteTour(index: number): void {
    this.tourList.splice(index, 1);
  }

  editTour(index: number, newTour: Tour): void {
    this.tourList[index] = newTour;
  }
}

Output:

  1. Event list

Services

import { Injectable } from '@angular/core';
import { Event } from '../types';

@Injectable({
  providedIn: 'root',
})
export class EventService {
  private eventList: Event[] = [
    {
      eventName: 'Conference 2024',
    },
    {
      eventName: 'Music Festival',
    },
  ];

  getEventList(): Event[] {
    return this.eventList;
  }

  addEvent(event: Event): void {
    this.eventList.push(event);
  }

  deleteEvent(index: number): void {
    this.eventList.splice(index, 1);
  }

  editEvent(index: number, newEvent: Event): void {
    this.eventList[index] = newEvent;
  }
}

Output:

  1. Developer tool list

Services

import { Injectable } from '@angular/core';
import { DeveloperTool } from '../types';

@Injectable({
  providedIn: 'root',
})
export class DeveloperToolService {
  private toolList: DeveloperTool[] = [
    {
      name: 'Visual Studio Code',
    },
    {
      name: 'Git',
    },
  ];

  getToolList(): DeveloperTool[] {
    return this.toolList;
  }

  addTool(tool: DeveloperTool): void {
    this.toolList.push(tool);
  }

  deleteTool(index: number): void {
    this.toolList.splice(index, 1);
  }

  editTool(index: number, newTool: DeveloperTool): void {
    this.toolList[index] = newTool;
  }
}

Output:

  1. Framework list

Services

import { Injectable } from '@angular/core';
import { Framework } from '../types';

@Injectable({
  providedIn: 'root',
})
export class FrameworkService {
  private frameworkList: Framework[] = [
    {
      name: 'Angular',
      version: '14.2.0',
    },
    {
      name: 'React',
      version: '18.0.0',
    },
  ];

  getFrameworkList(): Framework[] {
    return this.frameworkList;
  }

  addFramework(framework: Framework): void {
    this.frameworkList.push(framework);
  }

  deleteFramework(index: number): void {
    this.frameworkList.splice(index, 1);
  }

  editFramework(index: number, newFramework: Framework): void {
    this.frameworkList[index] = newFramework;
  }
}

Output:

  1. Libraries list

Services

import { Injectable } from '@angular/core';
import { Library } from '../types';

@Injectable({
  providedIn: 'root',
})
export class LibraryService {
  private libraryList: Library[] = [
    {
      name: 'RxJS',
      version: '7.5.0',
    },
    {
      name: 'Lodash',
      version: '4.17.21',
    },
  ];

  getLibraryList(): Library[] {
    return this.libraryList;
  }

  addLibrary(library: Library): void {
    this.libraryList.push(library);
  }

  deleteLibrary(index: number): void {
    this.libraryList.splice(index, 1);
  }

  editLibrary(index: number, newLibrary: Library): void {
    this.libraryList[index] = newLibrary;
  }
}

Output:

Github link: https://github.com/devken4u/angular-50-ds-services.git

website url: https://angular-ds-50-services.web.app

More from this blog

Untitled Publication

28 posts