I am trying to create a validator for one of the fields in my form. This validator is created in a service, however, to create it I must make a get request that returns the value with which my validator must match, but since the server's response does not occur instantly when returning the value, it is always returned undefined. How can I stop the execution of the code until I get a response from the service?
constructor(private formBuilder: FormBuilder, private validators : ValidatorsService)
{
this.form = this.formBuilder.group({
max_range: [null, this.validators.getMaxRangeValidators()]
}
)
}
function in service ValidatorsService
public getMaxRangeValidators(): any[] {
let validators = [];
this.httpClient.get<Range>(this.url).suscribe(
(data)=>{
validators = [Validators.maxLength(data.maxLength)];
}
)
return validators;
}