Angular. Провайдер Factory
У меня есть провайдер фабрики, в котором в зависимости от флага нужно создавать сервис либо с данными из статичного файла JSON, либо брать данные с сервера. Реализация с JSON работает, а вот при обращении к serverService он пишет, что данный сервис undefined. Как правильно обратиться к serverService?
import { StudentDebugService } from "./student-debug.service";
import { StudentService } from "./student.service";
import { ServerService } from "./server.service";
const isNeedDebug: boolean = false;
const studentServiceFactory = (studentDebugService: StudentDebugService, serverService: ServerService) => {
if (isNeedDebug) {
return new StudentService(studentDebugService.students);
}
serverService.fetchData().subscribe( (students) => {
return new StudentService(students);
});
};
export let studentServiceProvider = {
provide: StudentService,
useFactory: studentServiceFactory,
deps: [ServerService, StudentDebugService]
};
StudentService
import {Inject, Injectable} from '@angular/core';
import { ServerService } from "./server.service";
export interface StudentsArgs {
id: number;
surName: string;
name: string;
middleName: string;
birthday: string;
averageRate: number;
}
@Injectable({providedIn: "root"})
export class StudentService {
constructor(@Inject(StudentService)public students: StudentsArgs[]) {
}
getStudents(): StudentsArgs[] {
return this.students;
}
}
ServerService
import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { Observable } from "rxjs";
@Injectable({providedIn: "root"})
export class ServerService {
constructor(private http: HttpClient) {
}
fetchData(): Observable<any> {
return this.http.get("http://localhost:3000/api");
}
}