在 angular2 中重置 ngIf 上的 ngModel 值 [英] Reset ngModel values on ngIf in angular2

查看:23
本文介绍了在 angular2 中重置 ngIf 上的 ngModel 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,它是一个数组元素.我想清除 ngIf 条件下每个元素的值.请在下面的 HTML 中找到:

 

<table id="table" class="table table-hover table-bordered table-mc-light-blue"><头><tr>第 1 列第 2 列</tr></thead><tr *ngFor="让集合项;"><td>{{item.col1}}</td><td><input type="text" class="form-control" [(ngModel)]="item.col2" #input="ngModel" name="input-{{i}}"></td></tr>

flag 设置为 false 时,我想清除 集合 的所有值.有一个初始化集合的选项,但我不想这样做,因为我有几个这样的集合.

任何帮助都是可观的!!

解决方案

重置表格(){如果(!this.flag){this.collection = new Array()}}

I have a model which is an array element. I want to clear the values of each element on ngIf condition.Please find below HTML :

         <div *ngIf="flag" >
            <table id="table" class="table table-hover table-bordered  table-mc-light-blue">
               <thead>
                   <tr>
                       <th>col 1</th>
                       <th>col 2</th>
                   </tr>
                </thead>
                <tr *ngFor="let item of collection;">
                     <td>{{item.col1}}</td>
                     <td>
                        <input type="text" class="form-control" [(ngModel)]="item.col2" #input="ngModel" name="input-{{i}}">
                     </td>
                 </tr>
             </table>
         </div>

On flag set to false, I want to clear all values of the collection. There is an option of initializing collection, but I don't want to do that as I have several such collections.

Any help would be appreciable!!

解决方案

<input type="radio" (change)="resetForm()"/>


resetForm(){
     if(!this.flag){
          this.collection = new Array()
     }
}

这篇关于在 angular2 中重置 ngIf 上的 ngModel 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆