Strapi API Service

Service

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root',
})
export class JobsService {
  private apiUrl = 'http://localhost:1337/api/jobs';

  constructor(private http: HttpClient) {}

  getJobs(): Observable<any> {
    return this.http.get(this.apiUrl);
  }
}

Component

import { CommonModule } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { JobsService } from 'src/app/services/jobs.service';

@Component({
  selector: 'app-jobs',
  templateUrl: './jobs.component.html',
  styleUrls: ['./jobs.component.scss'],
  standalone: true,
  imports: [CommonModule],
})
export class JobsComponent implements OnInit {
  jobs: any[] = [];

  constructor(private jobsService: JobsService) {}

  ngOnInit() {
    this.jobsService.getJobs().subscribe(
      (response) => {
        this.jobs = response.data; 
      },
      (error) => {
        console.error('Error fetching jobs', error);
      }
    );
  }
}

Template

<div *ngFor="let job of jobs">
  <h2>{{ job.attributes.Title }}</h2>
  <p>{{ job.attributes.Description }}</p>
  <p><strong>VĂ¡ros:</strong> {{ job.attributes.City }}</p>
</div>
Was this page helpful?