如何清除Angular 2 ng-if textarea的值,使其不被传递

时间:2018-11-12 21:12:40

标签: angular angular-ng-if data-persistence

现在,根据先前在先前选择框中输入的用户选择,我有两个文本区域通过* ngIf指令有条件地显示。效果很好,除了以下事实:如果它们返回并更改选择框的值,则文本区域会相应地切换,但是不会删除文本区域的先前值,而只是将其隐藏。

我尝试将在组件绑定中定义的函数添加到onchange事件,以将从视图隐藏的文本框的值重置为空字符串,但无济于事。数据仍然存在

<ss-multiselect-dropdown  (onchange)=" resetdcn()"   id="substatus" 
*ngIf="ddlCorrespondenceStatus == 'M'" class="report-multiselect"
                                 [options]="ddlCorrespondenceSubStatuses"                                     
[settings]="genericSearchMSDropdownSettings"
                                 [texts]="statusMSDropdownTextSettings"                                    
[(ngModel)]="selectedCorrespondenceSubStatuses"                                   
 (ngModelChange)="handleStatusSelection($event)">
        </ss-multiselect-dropdown>


        <ss-multiselect-dropdown  (onchange)=" resetdcn()"  
     *ngIf="ddlCorrespondenceStatus == 'G'" class="report-multiselect"
                                 [options]="ddlStatus"                                     
    [settings]="genericSearchMSDropdownSettings"
                                 [texts]="statusMSDropdownTextSettings"
                                 [(ngModel)]="selectedStatuses"                                     
    (ngModelChange)="handleStatusSelection($event)">
        </ss-multiselect-dropdown>
          </div>
        </td>
        <td>

      <textarea (ngModelChange)="handleCorrespondenceDcnSelection($event)"  
    *ngIf="ddlCorrespondenceStatus == 'M'" id="txtDcn"name="txtDcn" 
    type="text" 
    placeholder="Correspondondence DCN " class="form-control input-md" 
    [(ngModel)]="txtCorrespondenceDcn"></textarea>


      <textarea (ngModelChange)="handleClaimDcnSelection($event)" 
    *ngIf="ddlCorrespondenceStatus == 'G'" id="txtDcn"  name="txtDcn" 
    type="text" placeholder="Claim DCN " class="form-control input-md" 
    [(ngModel)]="txtDcn"></textarea>

2 个答案:

答案 0 :(得分:0)

resetdcn()内部,只需将文本区域模型值设置为null。它们是数据绑定的,因此您无需在此之外做任何特别的事情。

public resetdcn(){
   // ...

   this.txtCorrespondenceDcn = null;
   this.txtDcn = null;
}

答案 1 :(得分:0)

问题在于该函数未被正确调用,而不是函数本身。它必须是(ngModelChange)=“ resetdcn()”