简体   繁体   English

如何以角度2重置表单控件

[英]How to reset a form control in angular 2

We have a list of dropdowns and when ever a dropdown is changed, I will show a button by its side. 我们有一个下拉列表,当下拉列表发生变化时,我会在旁边显示一个按钮。 To do this, i am using formcontrol dirty feature. 为此,我使用formcontrol脏功能。 Initially the button is hidden, and when the dropdown becomes dirty, I will sho the button. 最初按钮是隐藏的,当下拉列表变脏时,我会按下按钮。

But once a button is clicked, all the remaining buttons should be hidden again along with other dropdowns reset to their initial value. 但是,一旦单击一个按钮,所有剩余的按钮应该再次隐藏,同时其他下拉菜单将重置为其初始值。 How to accomplish this task, because as per my understanding there is no way to make a dirty field as undirty. 如何完成这项任务,因为按照我的理解,没有办法让脏场变得不合时宜。

<div class="form-group">
                                <h4 >Person {{i+1}}</h4>
                                <div >
                                    <div ><label class="control-label">Position</label></div>
                                    <div>
                                        <select [ngModel]="staff.position" (ngModelChange)="newPosition=$event;btn.hidden=0" #select="ngModel" name="position" placeholder="position">
                                            <option *ngFor="let i of instituteObj.academic_staff;let i=index" [value]="i+1">{{i+1}}</option>
                                        </select>
                                    </div>
                                    <div #btn [hidden]="!select.dirty" class="academic-move"><button (click)="changeStaffPosition(staff.position,newPosition);btn.hidden=1" type="button" class="btn btn-primary ">Move</button></div>
                                </div>
                            </div><br>

and my component function are: 我的组件功能是:

changeStaffPosition(currentPosition,newPosition){
    if(currentPosition < newPosition){
      for(let staff of this.instituteObj.academic_staff){
        if(staff.position > currentPosition && staff.position <= newPosition){
          --staff.position;
        }
      }
      this.instituteObj.academic_staff[currentPosition-1]['position'] = newPosition;  
    }
    else{
      for(let staff of this.instituteObj.academic_staff){
        if(staff.position >= newPosition && staff.position < currentPosition){
          ++staff.position;
        }
      }
      this.instituteObj.academic_staff[currentPosition-1]['position'] = newPosition;
    }

    this.instituteObj.academic_staff.sort((a,b) => {
      if(a['position']<b['position']){return -1}
      if(a['position']>b['position']){return 1}
      return 0;
    });
}

Basically the problem i am trying to solve is to make the user set the ordering of a list using the dropdowns where every list item has a dropdown, once the user chooses a position and clicks move button, the list reorders with new order. 基本上我想解决的问题是让用户使用下拉列表设置列表的顺序,其中每个列表项都有一个下拉列表,一旦用户选择一个位置并单击移动按钮,列表就会按新订单重新排序。

You can call use AbstractControl.markAsPristing() 你可以调用use AbstractControl.markAsPristing()

select.markAsPristine();

To do it from your component class you need a reference to the control first like 要从组件类中执行此操作,首先需要引用控件

@ViewChild('select') select;

...

this.select.markAsPristine();

Just saw, this is only available in the new forms module of RC.4. 刚看到,这只适用于RC.4的新表格模块。 The docs don't show this yet. 文档还没有显示出来。 Maybe it only becomes available with RC.5 https://github.com/angular/angular/blob/3f08efa35dd334c61127fc8059b4d73b2bd0b866/modules/%40angular/forms/src/model.ts#L144 也许它只能用于RC.5 https://github.com/angular/angular/blob/3f08efa35dd334c61127fc8059b4d73b2bd0b866/modules/%40angular/forms/src/model.ts#L144

As a workaround in previous versions it was common to recreate the form, for example by wrapping it with an *ngIf="showForm" , set showForm to true initially. 作为以前版本中的解决方法,通常重新创建表单,例如通过使用*ngIf="showForm"包装它,最初将showForm设置为true For recreating, set it to false , invoke change detection, set it back to true invoke change detection again (by injecting ChangeDetectorRef and call detectChanges() on it) 对于重建,将其设置为false ,调用变化检测,将其设置回true再次调用变化检测(通过注射ChangeDetectorRef并调用detectChanges()就可以了)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM