【问题标题】:Angular 4 Reset Button Resets DropDown Default ValueAngular 4重置按钮重置下拉默认值
【发布时间】:2018-02-17 18:04:43
【问题描述】:

我有一个如下所示的表单:

<form class="row" name="powerPlantSearchForm" (ngSubmit)="f.valid && searchPowerPlants()" #f="ngForm" novalidate>
          <div class="form-group col-xs-3" >
            <label for="powerPlantName">PowerPlant Name</label>
            <input type="text" class="form-control-small" [ngClass]="{ 'has-error': f.submitted && !powerPlantName.valid }" name="powerPlantName" [(ngModel)]="model.powerPlantName" #powerPlantName="ngModel" />
          </div>
          <div class="form-group col-xs-3" >
            <label for="powerPlantType">PowerPlant Type</label>
            <select class="form-control" [(ngModel)]="model.powerPlantType" name="powerPlantType">
              <option value="" disabled>--Select Type--</option>
              <option [ngValue]="powerPlantType" *ngFor="let powerPlantType of powerPlantTypes">
                {{ powerPlantType }}
              </option>
            </select>
          </div>
          <div class="form-group col-xs-3" >
            <label for="organizationName">Organization Name</label>
            <input type="text" class="form-control-small" name="powerPlantOrganization" [(ngModel)]="model.powerPlantOrg" #organizationName="ngModel" />
          </div>
          <div class="form-group col-xs-3" >
            <label for="powerPlantStatus">PowerPlant Active Status</label>
            <select class="form-control" [(ngModel)]="model.powerPlantStatus" name="powerPlantStatus">
              <option value="" disabled>--Select Status--</option>
              <option [ngValue]="powerPlantStatus" *ngFor="let powerPlantStatus of powerPlantStatuses">
                {{ powerPlantStatus }}
              </option>
            </select>
          </div>
          <div class="form-group col-md-3 col-xs-4">
            <button [disabled]="loading" class="btn btn-primary">Search</button>
            <img *ngIf="loading" src="data:image/gif;base64,R0lGODlhEAAQAPIAAP///wAAAMLCwkJCQgAAAGJiYoKCgpKSkiH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQAAADMwi63P4wyklrE2MIOggZnAdOmGYJRbExwroUmcG2LmDEwnHQLVsYOd2mBzkYDAdKa+dIAAAh+QQJCgAAACwAAAAAEAAQAAADNAi63P5OjCEgG4QMu7DmikRxQlFUYDEZIGBMRVsaqHwctXXf7WEYB4Ag1xjihkMZsiUkKhIAIfkECQoAAAAsAAAAABAAEAAAAzYIujIjK8pByJDMlFYvBoVjHA70GU7xSUJhmKtwHPAKzLO9HMaoKwJZ7Rf8AYPDDzKpZBqfvwQAIfkECQoAAAAsAAAAABAAEAAAAzMIumIlK8oyhpHsnFZfhYumCYUhDAQxRIdhHBGqRoKw0R8DYlJd8z0fMDgsGo/IpHI5TAAAIfkECQoAAAAsAAAAABAAEAAAAzIIunInK0rnZBTwGPNMgQwmdsNgXGJUlIWEuR5oWUIpz8pAEAMe6TwfwyYsGo/IpFKSAAAh+QQJCgAAACwAAAAAEAAQAAADMwi6IMKQORfjdOe82p4wGccc4CEuQradylesojEMBgsUc2G7sDX3lQGBMLAJibufbSlKAAAh+QQJCgAAACwAAAAAEAAQAAADMgi63P7wCRHZnFVdmgHu2nFwlWCI3WGc3TSWhUFGxTAUkGCbtgENBMJAEJsxgMLWzpEAACH5BAkKAAAALAAAAAAQABAAAAMyCLrc/jDKSatlQtScKdceCAjDII7HcQ4EMTCpyrCuUBjCYRgHVtqlAiB1YhiCnlsRkAAAOwAAAAAAAAAAAA==" />
          </div>
          <div class="form-group col-md-3 col-xs-3">
            <button class="btn btn-primary" (click)="f.reset()">Reset</button>
          </div>
        </form>

如下所示的布局:

当我单击重置按钮时,下拉菜单的默认值消失 - 如下图所示。

如何确保即使在点击重置按钮后仍保留默认值?

有什么想法吗?

【问题讨论】:

    标签: javascript html angular typescript dropdown


    【解决方案1】:
    1. 从 f.reset() 中移除表单引用,改为 reset()。其中reset()为组件类方法:

      reset(){
          this.model.powerPlantType = '';
          this.model.powerPlantStatus = '';
          // and other input resettings too
        }
      

      然后改变

      <button type="button" (click)="reset()">Reset</button>
      

      DEMO

    1. 将按钮类型从“按钮”更改为“重置”:

      <button type="reset>Reset</button>
      

    Demo

    【讨论】:

      【解决方案2】:

      在元素列表中有一个附加值id = -1

      types:any[]=[
                      {id:-1,Name:'Select One'},
                      {id:1,Name:'abc'},
                      {id:2,Name:'abdfsdgsc'}
          ];
      

      HTML 看起来像

      <select [(ngModel)]="selectedElement.id">
           <option *ngFor="let type of types" [ngValue]="type.id"> {{type.Name}}</option>
      </select>
      

      重置时

      reset(){
         this.selectedElement = {id:-1,Name:'Select One'};
        }
      

      LIVE DEMO

      【讨论】:

      • 我会试试这个!列表中有附加值有什么好处?
      • 您将能够重置为默认值
      • 为什么我不能没有 id 元素?
      • 我在我的重置函数中这样做 this.model.powerPlantType = '';这还不够吗?
      • @sparkr 是的,这就足够了。您需要更多帮助吗?
      猜你喜欢
      • 1970-01-01
      • 2018-03-16
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      • 2020-01-12
      • 2011-07-24
      • 2013-02-18
      相关资源
      最近更新 更多