【问题标题】:Keep select field placeholder on form.reset() in angular 6在 Angular 6 中保留 form.reset() 上的选择字段占位符
【发布时间】:2019-12-03 07:32:02
【问题描述】:

This 链接帮助我在选择框中使用占位符。 所以我的代码是

<form (ngSubmit)="onSubmit(form)" #form="ngForm" class="form-sample form-position">

<div class="form-group row">
  <label class="col-sm-3 col-form-label">Membership</label>
  <div class="col-sm-9">
    <select name="membership" [(ngModel)]="membership" class="form-control" required>
      <option [ngValue]="undefined" disabled selected hidden> Please select one option </option>

      <option>Free</option>
      <option>Professional</option>
    </select>
  </div>
</div>

 <div class="form-group row">
      <label class="col-sm-3 col-form-label">State</label>
      <div class="col-sm-9">
        <select [(ngModel)]="state" class="form-control" name="state" required=""
          placeholder="Select">
          <option [ngValue]="undefined" disabled selected hidden> Please select one option </option>
          <option *ngFor="let item of statesList">{{item}}</option>
        </select>
      </div>
    </div>

  <div class="text-center container-fluid form-group">
     <button [disabled]="!form.valid" type="submit" class="btn btn-primary btn-fw text-center">Submit</button>
     <button type="button" class="btn btn-secondary btn-fw" (click)="form.reset()">Clear</button>
  </div>
</form>

这是我的代码的一小部分。上面的代码工作得很好。当页面加载时选择字段Membership 默认选项Please select one option 被选中。

但问题是当我重置表单时,文本 Please select one option 也会被清除。但我希望在表单重置后保持选中此默认选项。重置我使用的表单form.reset()

【问题讨论】:

  • form.reset() 在做什么?
  • 要重置表单,我们只需要在 myform 模型上调用函数 reset()。 #form="ngForm" 我在表单标签中使用过它。所以form.reset() 会重置表单
  • myform model 是如何定义的?
  • 我在我的问题中添加了几行表单定义。看看

标签: javascript angular angular-forms


【解决方案1】:

form.reset()null 值设置为基础模型。由于您将 undefined 指定为默认选项的值,因此它们不匹配。要么提供用于清除表单的自定义逻辑,要么在重置时将模型值设置为undefined

<button type="button" class="btn btn-secondary btn-fw" (click)="clearForm()">Clear</button>

在你的 component.ts 中

clearForm() {
  this.membership = undefined;
  this.state = undefined;
}

将相应模型的选项值和初始值更改为null

<option [ngValue]="null" disabled selected hidden> Please select one option </option>

在你的 component.ts 中

membership = null;
state = null;

这是第二个的演示https://stackblitz.com/edit/angular-ngvh9p

【讨论】:

  • 当我将成员资格或状态设置为 null 时,默认文本 Select one option 即使在页面加载时也不可见,即第一次也不可见。
  • 您还必须将它们的初始值分配给null,以便在首次加载时选择默认值。和上面的演示完全一样。
  • membership = null;state = null; 当您第一次在 ts 文件中定义它们时。
  • 您是否在 html 中设置了[ngValue]="null"?喜欢&lt;option [ngValue]="null" disabled selected hidden&gt; Please select one option &lt;/option&gt;
  • 是的..它的工作..谢谢..我错过了这部分..非常感谢你
【解决方案2】:

如果你改变了select中的值,需要在表单重置时将membership的值设置为undefined。我不知道form.reset() 做了什么,但它可能会将值设置为null

【讨论】:

    【解决方案3】:

    我有一个类似的问题。

    在新选项卡中打开页面会在初始加载时显示 placeholder/default 值,然后离开页面并返回,它是空白的。

    我必须添加 [ngValue = "null"] 并将我的模型中的值最初设置为 'null'

    只有在那之后才出现。

    错误代码:

    <select name="title" [(ngModel)]="model.title">
        <option selected disabled> PLACEHOLDER </option>
        <option *ngFor="let title of titles" [value]="title"> title </option>
    </select>
    

    在component.ts中初始值设置为model.title = '',即'undefined'。

    通过添加显式 ngValue 并将其设置为“null”来修复代码:

    <select name="title" [(ngModel)]="model.title">
        <option [ngValue]="null" selected disabled> PLACEHOLDER </option>
        <option *ngFor="let title of titles" [value]="title"> title </option>
    </select>
    

    同时设置model.title = null的初始值。

    【讨论】:

    • 考虑添加一个代码示例来改进您的 anwser
    猜你喜欢
    • 2019-01-27
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    相关资源
    最近更新 更多