如何将输入字段的默认值绑定到变量?

Tom*_*mmy 4 default-value input-field property-binding angular

单击我的Angular App的UI中的某个按钮时,将打开一个对话框,用户可以在其中更新他/她的个人信息。为了简单起见,我将以下代码限制为他/她的用户名。也就是说,我假设用户只能在对话框中更改其用户名。

打开对话框的代码如下:

openDialog() {

    this.dialog.open(UpdatePersonalDataComponent , {data: {
      firstName: this.firstName,
      username: this.username 
      }
    });

 }
Run Code Online (Sandbox Code Playgroud)

文件update-personal-data.component.ts如下所示:

import { Component, OnInit, Inject } from '@angular/core';
import { MAT_DIALOG_DATA } from '@angular/material';


@Component({
  selector: 'app-consultants-update-dialog',
  templateUrl: './consultants-update-dialog.component.html',
  styleUrls: ['./consultants-update-dialog.component.css']
})
export class UpdatePersonalDataComponent implements OnInit {


  constructor(@Inject(MAT_DIALOG_DATA) public data: any) { }

  ngOnInit() {

  }

}
Run Code Online (Sandbox Code Playgroud)

目前,文件update-personal-data.component.html如下所示:

<form #f="ngForm" (ngSubmit)="onSubmit(f)" fxLayout="column" fxLayoutAlign="center center"> 
    <h1 mat-dialog-title>Hi, {{ data.firstName }}. Update your username below.</h1>
    <mat-dialog-content fxLayout="column" fxLayoutAlign="center">
        <mat-form-field>
          <input 
            matInput 
            ngModel 
            #userName="ngModel" 
            name="userName" 
            type="text" 
            value="{{data.username}}"
            required>
        </mat-form-field>
    <mat-dialog-content   
    <mat-dialog-actions>
        <button mat-raised-button color="primary" [mat-dialog-close]>Cancel</button>   
        <button type="submit" mat-raised-button color="primary" [mat-dialog-close] [disabled]="f.invalid">Submit</button>    
    </mat-dialog-actions>
 </form>
Run Code Online (Sandbox Code Playgroud)

如您所见,我想使用以下代码将输入字段的默认值设置为等于旧用户名:

value="{{data.username}}"
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用。输入字段为空。我也尝试了其他几种方法(例如[value] =“ {{data.username}}”),但没有任何效果。有谁知道如何将输入字段的默认值设置为等于变量data.username的值(已传递给对话框)?

Wil*_*era 7

我认为更好的方法是使用ReactiveForms模块的FormGroup。这样的事情。

在您的组件中。

 public form: FormGroup = new FormGroup({
    userName: new FormControl(''),
  });
Run Code Online (Sandbox Code Playgroud)

在您的HTML中

<form [formGroup]="form">

<mat-form-field class="full-width">
        <input autocomplete="off" required matInput
               formControlName="userName" placeholder="Username">
</mat-form-field>

</form>
Run Code Online (Sandbox Code Playgroud)

现在,您可以控制FORM来执行此操作。

this.form.setValue({
  userName: YOUR_VALUE,
});
Run Code Online (Sandbox Code Playgroud)


小智 7

这将起作用,使用 ngmodel

<input matInput
[(ngModel)]="data.username" 
#userName="ngModel" 
name="userName" 
type="text" 
required>
Run Code Online (Sandbox Code Playgroud)


san*_*Ram 5

我知道我参加聚会迟到了,但这对将来的任何人都有帮助。

正如@William Aguera给出的答案,我只是在编辑答案的最后一部分

this.form.patchValue({
userName: YOUR_VALUE,
});
Run Code Online (Sandbox Code Playgroud)

在这里,我只 用patchValue替换了setValue。我经常在编辑表单中使用它。


den*_*oha 5

如果您不想使用 formGroup,请在 HTML 中简单使用以下内容 <input>

<mat-form-field>
   <input matInput [value]="username"
          type="text" required>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)

还有你的组件:

export class UpdatePersonalDataComponent implements OnInit {

  constructor(@Inject(MAT_DIALOG_DATA) public data: any) {

    this.username = data.username;

  }

  ngOnInit() {

  }
Run Code Online (Sandbox Code Playgroud)