Angular: Services
Define an @Injectable():
import {Injectable} from '@angular/core'
import { HttpClient } from '@angular/common/http'
@Injectable()
export class EventService {
constructor(private http: HttpClient) { }
getEvents() { return this.http.get('https://localhost:8888/api/events'); }
}
Use it as a dependency:
import { Component, OnInit } from "@angular/core"
import { EventService } from '../../services/event.service'
interface EventResponse {
status: string
data: string
}
interface Event {
uuid: number
name: string
msg: string
}
@Component({
selector: "event",
template: `
<main>
<div id="flex-wrapper">
<table>
<thead>
<tr>
<th>UUID</th>
<th>Name</th>
<th>Message</th>
</tr>
</thead>
<tobdy>
<tr *ngFor="let event of events">
<td>{{event.uuid}}</td>
<td>{{event.name}}</td>
<td>{{event.msg}}</td>
</tr>
</tobdy>
</table>
</div>
</main>
`,
styles: []
})
export class EventComponent implements OnInit {
//Make array to hold data
public events: Event[] = [];
//Inject the relevant service here
constructor(private _eventService: EventService) { }
ngOnInit() { this.getEvents(); }
getEvents() {
// Casting response Objects
this._eventService.getEvents().subscribe((res: Object) => {
const E_R = res as EventResponse
const ALL_EVENTS = JSON.parse(E_R.data) as Event[]
const SLICED_EVENTS = ALL_EVENTS.slice(0, 5);
this.events = SLICED_EVENTS;
console.log(this.events);
});
}
}
Export the Module and Component:
import { NgModule } from "@angular/core"
import { CommonModule } from "@angular/common"
import { EventComponent } from "./event.component"
@NgModule({
imports: [
CommonModule
],
declarations: [EventComponent],
exports: [EventComponent]
})
export class EventModule { }
Resources and Links
Code samples:
Angular: Routing
Provided that a Module and its Components are correctly Exported:
import { NgModule } from "@angular/core"
import { Routes, RouterModule } from "@angular/router"
import { HomeComponent } from "./modules/home/home.component"
import { EventComponent } from "./modules/events/event.component"
const routes: Routes = [
{
path: "",
component: HomeComponent
},
{
path: "event",
component: EventComponent
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
Make sure to make Modules visible within the app itself:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {HttpClientModule} from "@angular/common/http"
import { AppComponent } from './app.component';
import { EventComponent } from './modules/events/event.component';
import { AppRoutingModule } from "./app.routing";
import { NavModule } from "./modules/nav/nav.module";
import {EventService} from "./services/event.service";
@NgModule({
declarations: [
AppComponent,
EventComponent
],
imports: [
BrowserModule,
AppRoutingModule,
NavModule,
HttpClientModule
],
providers: [EventService],
bootstrap: [AppComponent]
})
export class AppModule { }
import {Component} from "@angular/core"
@Component({
selector: "ng-root",
template: `
<div>
<custom-header></custom-header>
<router-outlet></router-outlet>
<custom-footer></custom-footer>
</div>
`,
styles: []
})
export class AppComponent {
}
Resources and Links
Code samples:
- https://github.com/Thoughtscript/mearn_2024/blob/main/angular/src/app/app.routing.ts
- https://github.com/Thoughtscript/mearn_2024/blob/main/angular/src/app/app.module.ts
- https://github.com/Thoughtscript/mearn_2024/blob/main/angular/src/app/app.component.ts
- https://github.com/Thoughtscript/pyng_2025/tree/main/ng