【问题标题】:How to bind default value of input field to variable?如何将输入字段的默认值绑定到变量?
【发布时间】:2018-11-24 20:11:50
【问题描述】:

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

打开对话框的代码如下:

openDialog() {

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

 }

文件 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() {

  }

}

文件 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>

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

value="{{data.username}}"

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

【问题讨论】:

    标签: angular default-value input-field property-binding


    【解决方案1】:

    如果您不想使用 formGroup,只需在 HTML 中使用以下&lt;input&gt;

    <mat-form-field>
       <input matInput [value]="username"
              type="text" required>
    </mat-form-field>
    

    还有你的组件:

    export class UpdatePersonalDataComponent implements OnInit {
    
      constructor(@Inject(MAT_DIALOG_DATA) public data: any) {
    
        this.username = data.username;
    
      }
    
      ngOnInit() {
    
      }
    

    【讨论】:

      【解决方案2】:

      我知道我迟到了,但这对以后的任何人都有帮助。

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

      this.form.patchValue({
      userName: YOUR_VALUE,
      });
      

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

      【讨论】:

      • setValue 如果它是您表单中的唯一项目,则它可以工作。当您只想设置某些表单元素时,patchValue 是有效的。
      【解决方案3】:

      这会起作用,使用ngmodel

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

      【讨论】:

      • 无法绑定到“ngModel”,因为它不是“输入”的已知属性 - 我收到此错误。谢谢
      【解决方案4】:

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

      在您的组件中。

       public form: FormGroup = new FormGroup({
          userName: new FormControl(''),
        });
      

      在你的 HTML 中

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

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

      this.form.setValue({
        userName: YOUR_VALUE,
      });
      

      【讨论】:

      • 我试图在 matInput 文本框中设置一个默认值:tribeForm: FormGroup = new FormGroup({tribeName: new FormControl('WOW default'), });但它不起作用。我将只使用 matSelect。感谢这个“this.form.setValue”
      猜你喜欢
      • 2017-08-04
      • 2021-08-13
      • 1970-01-01
      • 2011-08-04
      • 2015-04-11
      • 1970-01-01
      • 2012-06-04
      • 1970-01-01
      相关资源
      最近更新 更多