【问题标题】:How to Handle reactive forms with html select?如何使用 html select 处理反应式表单?
【发布时间】:2017-07-14 01:15:23
【问题描述】:

我有一个这样的模板:

<form [formGroup]="form">
   <mdl-textfield type="text" #userFirstName name="lastName"
              label="{{'FIRSTNAME' | translate}}" pattern="[A-Z,a-zéè]*"
              error-msg="{{'FIRSTNAME' | translate}}" floating-label
              formControlName="firstName">
  </mdl-textfield>
  <mdl-textfield type="text" #userLastName name="firstName" pattern="[A-Z,a-z,éè]*"
             label="{{'LASTNAME' | translate}}"                       
             error-msg="{{'LASTNAME' | translate}}" floating-label                                                          
             formControlName="lastName">
  </mdl-textfield>
  <mdl-textfield type="text" name="town" #userTown label="{{'TOWN' | translate}}"
                pattern="[A-Z,a-z,éè]*"
                error-msg="{{'TOWN' | translate}}" floating-label
                formControlName="town">
  </mdl-textfield>
  <div class="mdl-textfield mdl-js-textfield"> {{ 'COUNTRY' | translate }}
   <select name="country">
     <option *ngFor="let country of countries" [ngValue]="country"          
         [selected]="country.id" 
         formControlName="countryId" ngDefaultControl>{{country.countryName}}
     </option>
   </select>
  </div>
  <mdl-card-actions>
    <button (click)="modifyClient()" [disabled]="!form.valid" 
         class="mdl-button mdl-button--raised mdl-button--accent">  
         {{'MODIFY' | translate}}
    </button>
  </mdl-card-actions>
 </form>

带有文本字段和选择。实际上我有 2 个选择,但为了缩短代码,我在这里取消了一个。

我的组件是:

public form: FormGroup;
    public countries;
    public languages;
    private id;
    private user: Client = new Client();
    public firstName = new FormControl('');
    public lastName = new FormControl('');
    public town = new FormControl('');
    public company = new FormControl('');
    public countryId = new FormControl('');
    public languageId = new FormControl('');
 ngOnInit()
    {
        this.getCountries();
        this.getLanguages();
        this.id = this.clientApi.getCachedCurrent().id;
        this.form = this.fb.group({
            'firstName': this.clientApi.getCachedCurrent().firstName,
            'lastName': this.clientApi.getCachedCurrent().lastName,
            'town': this.clientApi.getCachedCurrent().town,
            'company': this.clientApi.getCachedCurrent().company,
            'countryId': this.clientApi.getCachedCurrent().countryId,
            'languageId': this.clientApi.getCachedCurrent().languageId
            });
        this.form.valueChanges
            .map((formValues) => {
                formValues.firstName = formValues.firstName.toUpperCase();
                return formValues;
            })
            // .filter((formValues) => this.form.valid)
            .subscribe((formValues) => {
                console.log(`Model Driven Form valid: ${this.form.valid} value:`, JSON.stringify(formValues));
            });
    }

所以这里一切正常,信息来自后端,效果很好。 当我更改文本字段上的值时,会调用 this.form.valueChanges 并将 formValues 写入控制台。 我的问题是选择标签。如果我在默认情况下选择另一个,则表单没有变化,并且 this.form.valueChanges 不会被调用...

知道如何解决这个问题吗?

感谢和问候,

【问题讨论】:

    标签: javascript html angular reactjs typescript


    【解决方案1】:

    formControlName 应该在 &lt;select&gt; 上,但你在 &lt;option&gt; 上拥有它。

    【讨论】:

    • 完美运行。我应该看到的。谢谢。
    • 注意不要在模板中设置选项为选中状态,设置formControl的值,它会自动选择选项。
    • 你确定吗?我有: 和 this.form = this.fb.group({ 'countryId': this.clientApi.getCachedCurrent().countryId }); 已正确初始化,但确实如此不行...:(
    • 你的每个选项都应该有不同的值,你的所有选项都有相同的值,countryId。试试[value]="country.id",如果不起作用,请提出一个新问题,以便对搜索类似问题的其他人有所帮助
    猜你喜欢
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 2013-03-05
    相关资源
    最近更新 更多