2

I want to show my strings array in select dropdown. I tried the normal way :

<form [formGroup]="emailForm" #form="ngForm" (ngSubmit)="sendEmail()" novalidate class="tooltip-center-bottom">
  <div class="form-group">
    <select id="emailTem" class="form-control" formControlName="emailTem" [(ngModel)]="emailTem">
      <option value="" disabled selected>Select Email Template</option>
      <option *ngFor="let obj of emailArray" [ngValue]="obj">{{obj}}</option>    
    </select>
    <div *ngIf="statusForm.get('status').errors?.required && form.submitted" class="invalid-tooltip">Status is required!</div>
  </div>
  <button class="btn btn-primary status-button" type="submit">Submit</button>
</form>

The array i have assigned :

emailArray: string[] = [
    'Call Not Connected',
    'Refund'
];

it was working before normal. But now it's not showing blank

dropdown

if i try with normal by setting values directly like this : <option value="New" ng-reflect-value="New">New</option> it works properly

Even stackbliz not working

8
  • Why do you use reactive form and template driven form together? I think you can remove #form="ngForm". Please check emailArray object in the console.log() on the ts. Commented Feb 17, 2021 at 5:10
  • @oz1985oz i usually don't work as frontend developer and new to angular framework but i read it helps to assign values retrived from api and formcontrol help me to set validator for control Commented Feb 17, 2021 at 5:13
  • it's printing properly. i just printed the with normal stringify Commented Feb 17, 2021 at 5:16
  • They both (reactive form and template driven form) are the same. They contain the same information / methods. The main difference is template driven form is mostly on HTML and reactive form is mostly on ts. Commented Feb 17, 2021 at 5:19
  • @oz1985oz does it affect my select option because i was using before and it was working great. But when i tried to update the page. It stopped working Commented Feb 17, 2021 at 5:20

4 Answers 4

1
<form #form="ngForm" (ngSubmit)="sendEmail()" class="tooltip-center-bottom">
  <div class="form-group">
    <select id="emailTem" class="form-control" name="email" [(ngModel)]="emailTem">
      <option value="" disabled selected>Select Email Template</option>
      <option *ngFor="let obj of emailArray" [value]="obj">{{obj}}</option>    
    </select>
  </div>
  <button class="btn btn-primary status-button" type="submit">Submit</button>
</form>
Sign up to request clarification or add additional context in comments.

3 Comments

<select id="emailTem" class="form-control" formControlName="emailTem" [(ngModel)]="emailTem"> What is the need of using both the formControlName and [(ngModel)]. You are trying to use both the template driven and reactive form . Please remove [(ngModel)].
I am not a frontend developer but i assigned values retrieved from api.
Somehow it worked on stackbliz but not on my computer
1

Please try with...

.html file

<form [formGroup]="emailForm" #form="ngForm" (ngSubmit)="sendEmail()" novalidate class="tooltip-center-bottom">
<div class="form-group">
    <select id="emailTem" class="form-control" formControlName="emailTem" >
  <option value="" disabled selected>Select Email Template</option>
  <option *ngFor="let obj of emailArray" [ngValue]="obj">{{obj}}</option>
</select>
    <!-- <div *ngIf="emailForm.get('email').errors?.required && form.submitted" class="invalid-tooltip">Status is
        required!</div> -->
</div>
<button class="btn btn-primary status-button" type="submit">Submit</button>

there is problem with validation So remove or comment that part and it will work fine Soluation hope it will work fine !

6 Comments

Nope it's not working same output just blank options
@Ashish Okay give me 2 min
@Ashish remove all [(ngModel)] inside <form> tag
Not working please check my stackbliz i added so other can contribute
@Ashish please check updated answer there is problem with your code
|
0

You had a typo get('email'):

<div *ngIf="emailForm.get('email').errors?.required && form.submitted" class="invalid-tooltip">Status is
            required!</div>

Should be get('emailTem'):

<div *ngIf="emailForm.get('emailTem').errors?.required && form.submitted" class="invalid-tooltip">Status is
            required!</div>

2 Comments

But it doesn't solve my problem. My main problem was it didn't show my list
Did you change it as above and it still didn't work? I found it out in your Stackblitz. With the typo there is an error in the console and from then on things break
0

Try this Angular Reactive form with validation and get value on submit -

app.component.html -

<form [formGroup]="emailForm" (ngSubmit)="sendEmail()" novalidate class="tooltip-center-bottom">
    <div class="form-group">
        <select id="emailTem" class="form-control" formControlName="emailTem">
      <option value="" disabled selected>Select Email Template</option>
      <option *ngFor="let obj of emailArray" [value]="obj">{{obj}}</option>    
    </select>
        <br><br>
        <div *ngIf="formSubmit && emailTem.errors">
            <div class="text-danger" *ngIf="emailTem.errors.required">Status is required!</div>
        </div>
        <br>
    </div>
        <button class="btn btn-primary status-button" type="submit">Submit</button>
</form>

app.component.ts -

import { FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators } from "@angular/forms";

export class AppComponent {
    emailForm: FormGroup;
    formSubmit: boolean = false;
    emailArray: string[] = ["Call Not Connected", "Refund"];

    ngOnInit(): void {
        this.emailForm = new FormGroup({
          emailTem: new FormControl("", [Validators.required])
        });
    }

    sendEmail() {
        this.formSubmit = true;
        if(this.emailForm.valid){
            console.log(this.emailForm.value.emailTem); // selected value
        }
    }

    get emailTem() {
        return this.emailForm.get("emailTem");
    }
}

app.module.ts -

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';

@NgModule({
  imports:      [ BrowserModule, FormsModule, ReactiveFormsModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

Example -

angular-reactive-form

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.