Blog>
Snippets

Creating a Basic Angular Plugin

Define a simple Angular Service as a plugin and show how to register it within an Angular module to be reused across the application.
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class SimpleService {
  constructor() {}

  sayHello() {
    console.log('Hello from Simple Service!');
  }
}
Defines an Angular service called SimpleService, which includes a method called sayHello that logs a greeting message to the console. The @Injectable decorator with the 'root' option means that this service will be a singleton and available app-wide.
import { NgModule } from '@angular/core';
import { SimpleService } from './simple.service';

@NgModule({
  providers: [SimpleService]
})
export class SimpleServiceModule {}
Creates an Angular module called SimpleServiceModule that registers the SimpleService so it can be used by other components, directives, or services throughout the application.