Для внедрения пользовательских сервисов в динамическую исходную модель управления формой c необходимо передать соответствующий сервис в аргумент конфигурации модели.
Шаг 1
Создать сервис в соответствии с вам нужно создать поддельный ConfigService.
import { HttpClient} from "@angular/common/http"
import { Injectable } from "@angular/core"
@Injectable()
export class ConfigService {
constructor(private http: HttpClient) { }
configUrl = 'assets/config.json';
getConfig() {
return this.http.get(this.configUrl);
}
}
Шаг 2
Давайте создадим модель, которая расширена с помощью FormControlConfig для асинхронной c привязки источника.
import { FormControlConfig } from "@rxweb/reactive-dynamic-forms"
import { ConfigService } from "./config.service"
export class SourceAsyncConditionalModel extends FormControlConfig{
constructor(fieldConfig: { [key: string]: any }, public controlsConfig: { [key: string]: FormControlConfig },notificationId:number,private configService:ConfigService){
super(fieldConfig,controlsConfig,notificationId);
}
filter() {
let promise = new Promise<any>((resolve, reject) => {
/// call the service
if(this.configService)
this.configService.getConfig();
});
}
}
Если вы видите выше код, где я определил четыре параметра. Первые три параметра используются в FormControlControl, а четвертый параметр мы можем использовать в экземпляре модели.
Шаг 3
Теперь мы должны передать параметр соответствующей модели. См. Ниже код:
this.dynamicFormConfiguration = {
controlConfigModels: [{ modelName: 'sourceAsync', model: SourceAsyncConditionalModel,arguments:[this.configService] }],
}
this.dynamicFormBuildConfig = this.formBuilder.formGroup(this.serverData, this.dynamicFormConfiguration);
Вот рабочий пример