Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

218
Visualizações
Dynamic Angular Forms on User Interaction

I have a dropdown that the user uses to switch between different Angular forms.

HTML

<form [formGroup]="evalForm">
    <mat-form-field appearance="fill">
        <mat-label>Select Analysis Group</mat-label>
            <mat-select (selectionChange)="groupSelected($event)">
                    <mat-option *ngFor="let item of groupDropdown | async" [value]="item.value">{{item.text}}</mat-option>
            </mat-select>
    </mat-form-field>

    <ng-container *ngFor="let groups of groupSections;">
        <input type="text" [formControlName]="groups?.name">
    </ng-container>
</form>

What should the Angular look like to get the reactive forms working when someone selects an item in the dropdown?

What groupSelected() currently does (basic non-working pseudo-code):

groupSelected(selectedItem) {

    // 1) Grabs the FormControlNames from the DB
    const formControlNamesList = getItemsFromDB(selectedItem);

    // 2) Sets FormControlNames for DOM
    // [{groups:{name: 'keyOne'}}, {groups:{name: 'keyTwo'}},{groups:{name: 'keyThree'}}]
    this.groupSections = setGroupSections(formControlNamesList);

    // 3) Creates a formControlNamesList Object in the form
    // {"keyOne": FormControl, "keyTwo": FormControl, "keyThree": FormControl}
    const formObj = formatForAngularForms(formControlNamesList);

    // 4) Set the Reactive Form
    this.evalForm = this.fb.group(new FormGroup(formObj));

}

formatForAngularForms(formControlNamesList) {
    const formObj = {};
    for(let i=0;i<formControlNamesList.length;i++) {
        formObj[formControlNamesList[i].name] = new FormControl(null,[Validators.required]);
    }
    return formObj;
}

At the moment, the console explodes with errors about how it can't find the FormControlName and I suspect it has to do with the timing of when everything gets rendered, but I'm not entirely sure if that's it or not. I need the forms to render completely new forms each time a new value selected from the dropdown. The UI will always be dynamic and each time the dropdown changes the input FormControlNames could be different based on what the DB sends.

I've read that maybe FormArray might be something use but I can't find an example anywhere that matches what I'm looking to do.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Shouldn't you wait for the answers from backend before proceeding to build the form? If getItemsFromDB() returns an observable you could do async/await:

async groupSelected(selectedItem) {
    const formControlNamesList = await getItemsFromDB(selectedItem);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda