首页 > 解决方案 > Angular 6 Mat-Select does not define user.email through http GET request

问题描述

I want to make a select with a FormGroup. The data that the user can select from comes through a GET request.

Adminpanel.ts

this.userservice.getallusers().subscribe((res: any) =>{
    for (let index = 0; index < res.length; index++) {
      this.users[index] = res[index];
      this.usersemails[index] = this.users[index].email;
      console.log(this.usersemails[index]);
    }

});

The data goes through a for loop and gets assigned to array usersemails[index]. The array gets filled up correct.

FormGroup from HTML

<form [formGroup]="AddxpForm" (ngSubmit)="addxponSubmit()">
    <mat-form-field class="geo-full-width">
      <input matInput placeholder="Amount of XP" type="number" name="User_xp" formControlName="user_xp">
    </mat-form-field>
    <mat-select placeholder="User email" name="useremail" fromControlName="useremail">
      <mat-option *ngFor="let useremail of usersemails" [value]="useremail">
        {{useremail}}
      </mat-option>
    </mat-select>
    <button mat-raised-button class="geo-full-width" type="submit" [disabled]="!AddxpForm.valid">Add UserXP</button>
  </form>
</div>

The select works also because I can select the emails I get through the HTTP GET request. But when I try to post it again the body email stays undefined.

Submit function from ts file

addxponSubmit(){
  this.user.email = this.AddxpForm.value.useremail;
  this.user.experience = this.AddxpForm.value.user_xp
  console.log(this.AddxpForm);
  console.log(this.user);
}

can someone tell me what I do wrong here ?

标签: angularrestangular-materialmean-stack

解决方案


正如 fridoo 评论的那样,问题是错字,您可以在此处检查表单是否正常工作:

https://stackblitz.com/edit/mat-select-stack?embed=1&file=src/app/app.component.ts


推荐阅读