简体   繁体   English

Angular2 select 表单未捕获值

[英]Angular2 select form not capturing value

I'm using Angular 14 trying to create a select form reading the options from an array in my component.我正在使用 Angular 14 尝试创建一个 select 表单,从我的组件中的数组中读取选项。 The select options are correctly populated, but when I send the form, the selected option value is not being sent. select 选项已正确填充,但当我发送表单时,未发送选定的选项值。 It just keep it empty.它只是让它空着。 Any suggestions about what could be causing this?关于可能导致此问题的任何建议?

<mat-label>Perfil</mat-label>
    <mat-select name="perfil">
       <mat-option *ngFor="let perfil of perfis" 
       [value]="perfil.value">{{ perfil.title }}
       </mat-option>
    </mat-select>

In the component the array is:在组件中,数组是:

  perfis: {title: string, value: number }[] = [
      { "title": "Admin", "value": 1 },
      { "title": "Coordenador", "value": 2 },
      { "title": "Colaborador", "value": 3 },
    ];

Inside form component's constructor:内部表单组件的构造函数:

this.form = this.formBuilder.group({
    id: [''],
    login: [''],
    password: [''],
    nome: [''],
    rg: [''],
    cpf: [''],
    telefone: [''],
    email: [''],
    ativo: [''],
    perfil: ['']
  });

To submit the form:提交表格:

onSubmit() {
console.log(this.form.value); // this is where I see the field is always empty
this.usersService.save(this.form.value)
.subscribe(
  data => this.onSuccessInsertion('Informação inserida do banco de dados.'),
  error => this.onError('Não foi possível salvar esta informação no banco de dados.')
  );

} }

Maybe I'm listing the options but I'm not capturing the value after being selected?也许我列出了选项,但在被选中后我没有捕捉到价值? If so, how should I do this?如果是这样,我应该怎么做? I've read that you could use [selected], but that was not even recognized by Angular.我读到过您可以使用 [selected],但 Angular 甚至无法识别。

I noticed that maybe it is because the value is being captured as a string.我注意到这可能是因为该值被捕获为字符串。 I read in another post that [value]="..." only supports string values and [ngValue]="..." supports any type So it would be needed to use [ngValue]="perfil.value" in the HTML, but it gives me error if I [ngValue] instead of [value].我在另一篇文章中读到 [value]="..." 仅支持字符串值,而 [ngValue]="..." 支持任何类型因此需要在 [ngValue]="perfil.value" HTML,但如果我 [ngValue] 而不是 [value],它会给我错误。 The error message is:错误信息是:

Can't bind to 'ngValue' since it isn't a known property of 'mat-option'.无法绑定到“ngValue”,因为它不是“mat-option”的已知属性。

  1. If 'mat-option' is an Angular component and it has 'ngValue' input, then verify that it is part of this module.如果“mat-option”是一个 Angular 组件并且它有“ngValue”输入,则验证它是该模块的一部分。
  2. If 'mat-option' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.如果“mat-option”是一个 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以抑制此消息。
  3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.ngtsc(-998002)要允许任何属性,请将“NO_ERRORS_SCHEMA”添加到此组件的“@NgModule.schemas”中。ngtsc(-998002)

It seems the select is not associated with a control in the FormGroup .似乎 select 与FormGroup中的控件无关。 Because of this, Angular doesn't know that the select should change the perfil control in the form.因此,Angular 不知道 select 应该更改窗体中的perfil控件。 Assuming you set the FormGroup earlier in the template, you can bind the select to that control with the formControlName directive, like this:假设您在模板的前面设置了FormGroup ,您可以使用formControlName指令将 select 绑定到该控件,如下所示:

<mat-select name="perfil" [formControlName]="'perfil'">
    <mat-option *ngFor="let perfil of perfis"
    [value]="perfil.value">{{ perfil.title }}
    </mat-option>
</mat-select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM