简体   繁体   English

我如何获得 Angular 输入字段值?

[英]How I get Angular input field value?

I successfully passed data to my form input field like below.我成功地将数据传递到我的表单输入字段,如下所示。

<div class="input" >
      <mat-form-field appearance="outline" class="input-text">
      <mat-label>Name</mat-label>
      <input matInput placeholder="" style=" width: 100%;" value="{{userDetail.name}}"  >
      </mat-form-field>
    </div>

Output without formControl Output 没有 formControl

I want to update and get this input field value to update the database.我想更新并获取此输入字段值以更新数据库。 So I used "formControl" to get the input field value.所以我使用“formControl”来获取输入字段值。

 [formControl]="nameControl" 

But when I add this property I can't see the input field value.但是当我添加这个属性时,我看不到输入字段的值。 How I get this input field value?我如何获得这个输入字段值? Output with formControl Output 与 formControl

you can has a FormControl你可以有一个 FormControl

control=new FormControl()

<!--see that not use value and use [formControl]-->
<input matInput placeholder="" style=" width: 100%;" [formControl]="control">

And then, you can give value when create the control然后,您可以在创建控件时赋值

control=new FormControl('my value')

or using setValue或使用 setValue

this.control.setValue('my value')

or you can has a formGroup and inside a FromControl或者你可以有一个 formGroup 和一个 FromControl

form=new FormGroup({
  control:new FormControl();
}))

<form [formGroup]="form">
  <!--see that not use value else FormControlName (without [])-->
  <input matInput placeholder="" style=" width: 100%;" formControlName="control">
</form>

Again, you can give value when create the form同样,您可以在创建表单时赋予价值

form=new FormGroup({
  control:new FormControl('my value');
}))

or using setValue或使用 setValue

this.form.setValue({control:'my value'}) //to all the form
//or
this.form.get('control').setValue('my value') //only to the control of the formControl
//you can also using patchValue
this.form.patchValue({control:'my value'}) //to all the form

value="{{userDetail.name}}"

interpolation on attribute it's a bad practice: You should to use angular attribute and property binding instead:对属性进行插值是一种不好的做法:您应该改用 angular 属性和属性绑定:


How I get this input field value?我如何获得这个输入字段值?

Angular provides two different approaches to handling user input through forms: reactive and template-drive. Angular 提供了两种不同的方法来处理通过 forms 的用户输入:反应式和模板驱动。

template-drive模板驱动

public name: string;
<input matInput placeholder="" style=" width: 100%;" [(ngModel)]="name" />

reactive被动的

public nameControl = new FormControl();
<input matInput placeholder="" style=" width: 100%;" [formControl]="nameControl" />

in both cases, if you want to set an initial value, you have to do it yourself by operating on the field在这两种情况下,如果你想设置一个初始值,你必须自己通过在字段上操作来完成

template-drive模板驱动

public name = "initial name";

or要么

myMethod(): void {
    this.name = "initial name";
}

reactive被动的

public nameControl = new FormControl("initial name");

or要么

myMethod(): void {
    this.nameControl.setValue("initial name");
}

For more info about input binding, you can visit the official Angular documentation: https://angular.io/guide/forms-overview有关输入绑定的更多信息,您可以访问官方 Angular 文档: https://angular.io/guide/forms-overview

I successfully passed data to my form input field like below.我成功地将数据传递到我的表单输入字段,如下所示。

<div class="input" >
      <mat-form-field appearance="outline" class="input-text">
      <mat-label>Name</mat-label>
      <input matInput placeholder="" style=" width: 100%;" value="{{userDetail.name}}"  >
      </mat-form-field>
    </div>

Output without formControl没有 formControl 的输出

I want to update and get this input field value to update the database.我想更新并获取此输入字段值以更新数据库。 So I used "formControl" to get the input field value.所以我使用“formControl”来获取输入字段值。

 [formControl]="nameControl" 

But when I add this property I can't see the input field value.但是当我添加这个属性时,我看不到输入字段值。 How I get this input field value?我如何获得这个输入字段值? Output with formControl使用 formControl 输出

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

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