Angular2 *ngFor把数据显示在多个input中出错解决方法

点击添加按钮会自动添加一个空的input组

html

<div class="form-inline">
            <label class="form-control-label">属性</label>
            <button type="button" class="btn btn-test" (click)="addProperty()">添加</button>
        </div>
        <div class="properties" *ngIf="options.properties">
            <div class="property-inline" *ngFor="let property of options.properties; let idx=index">
                <div class="form-inline" >
                    <label class="form-control-label"></label>
                    <div class="input-group">
                        <div class="form-inline">
                            <input type="text" class="property-input" name="proName_field" [(ngModel)]="property.name">
                            <input type="{{property.secret ? ‘password‘ : ‘text‘}}" class="property-input" name="proValue_field" [(ngModel)]="property.value" >
                            <input type="checkbox" [(ngModel)]="property.secret" name="secret_field">
                            <a (click)="deleteProperty(idx)"><i class="fa fa-trash color-red"></i></a>
                        </div>
                    </div>
                </div>
            </div>
        </div>

js

addProperty() {
        const tempProperty = {
            name: ‘‘,
            value: ‘‘
        };
        if (!this.options.properties) {
            this.options.properties = [];
        }
        this.options.properties.push(tempProperty);
 }

    deleteProperty(index) {
        this.options.properties.splice(index, 1);
    }

出现的问题是,当我在第一行input中分别添加上数据xxx,ddd后,再次点击添加按钮,此时页面刷新,添加一个空的input行,此时有两行,而第一行input中填好的值却不见了,查看元素它的model属性明明又是绑定了刚输入的值的,页面上怎么没有显示呢?

通过查阅资料了解到在ng2表单中使用ngModel需要注意,必须带有name属性或者使用 [ngModelOptions]=”{standalone: true}”,二选其一,再看我的html代码中我把显示name的input的name属性设置为一个定值,这样再经过*ngFor后,显示不同name值的input的name属性都是一样的,是不是这个原因导致的呢,于是就试着把每个input的name属性设为不一样的值:

<div class="form-inline">
            <label class="form-control-label">属性</label>
            <button type="button" class="btn btn-test" (click)="addProperty()">添加</button>
        </div>
        <div class="properties" *ngIf="options.properties">
            <div class="property-inline" *ngFor="let property of options.properties; let idx=index">
                <div class="form-inline" >
                    <label class="form-control-label"></label>
                    <div class="input-group">
                        <div class="form-inline">
                            <input type="text" class="property-input" [name]="‘proName_field‘ + idx" [value]="property.name" [(ngModel)]="property.name">
                            <input type="{{property.secret ? ‘password‘ : ‘text‘}}" class="property-input" [name]="‘proValue_field‘+idx" [(ngModel)]="property.value" >
                            <input type="checkbox" [(ngModel)]="property.secret" [name]="‘secret_field‘+idx">
                            <a (click)="deleteProperty(idx)"><i class="fa fa-trash color-red"></i></a>
                        </div>
                    </div>
                </div>
            </div>
        </div>

问题解决了。。。。

时间: 2024-08-11 15:14:10

Angular2 *ngFor把数据显示在多个input中出错解决方法的相关文章

WebView中input file的解决方法

public class MyWb extends Activity { /** Called when the activity is first created. */ WebView web; ProgressBar progressBar; private ValueCallback<Uri> mUploadMessage; private final static int FILECHOOSER_RESULTCODE = 1; @Override protected void onA

js 实时监听input中值变化

js 实时监听input中值变化 分类: Javascript2014-05-11 11:13 849人阅读 评论(0) 收藏 举报 [html] view plaincopyprint? <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>RunJS

关于chrome下input中Enter的keydown事件会自动提交form的疑问与解决

当页面有<form>操作时,浏览器会有默认的响应enter键就提交form表单. 看html代码 <form action="http://www.7k7k.com"> <input type="text"> 当你在input框里输入后,按enter键,就会默认提交form, 但实际需求并不是这样的,比如在input输入时会有弹出的suggestion选项让用户选择,用户按了enter键后并不想直接提交,所以需要在部分逻辑下需要取

iphone中点击input不能选中input中的内容

点击一个input,通过它的click事件选中这个input框中的内容,这个用jquery挺好实现的,但是有一个问题,在PC端和android手机上都可以让功能正常实现,在iphone上就没有效果了,后查了好多资料发现在iphone中可以这样写 <script> $(function(){ //$('input').focus(focustext) /*******用以下三行代码即可实现*******/ $('input').click(function(){ this.selectionSt

input的onchange事件实际触发条件与解决方法

nput中onchange事件已经属于元老级别了,并且现在同onclick一样使用频率很高,然而onchange的机制实际上有很多童鞋并不清楚,我们通过实例来分析这个事件的特征. 触发onchange 首先页面有一个input标签,并且已绑定onchange事件,如: <input type="text" onchange="console.log(this.value);" /> 这个事件要做的动作很简单,只是把input的值在控制台上打印出来就好.效

将前台input中的数组异步传到后台并存入数据库

将前台input中的数组异步传到后台并存入数据库 先看图: 利用ajax异步交互数据,并不是以json数组的形式将数据传到后台,而是利用字符数组的形式将其传到后台.动态新增每一行数据,将每一列对应的数据存入数组,并传到后台中进行入库.(当然可以直接以json数组的形式将每一行的数据传到后台,这里就不详细讲了) 前台js代码: ///保存多行数据,运用ajax异步交互请求 function btnSave() { /*传值方式一:将添加的每一字段的值存在数组中,再将数组转换成字符串串传到后台*/

js下读取input中的value值

很多人(包括我),总想像以前操作js一样,读取到input中的值:document.getElementById('').value; 结果事实证明这样读到得是null. eval(document.getElementById('')).value

input,button制作按钮IE6,IE7点击时1px黑边框的解决方法

按钮在IE6中点击时1px黑边框的最常见的解决方法 首先设置按钮为none,然后在按钮外面套一层来实现边框的效果,部分代码如下 .btnbox{ border:solid 1px red;} .btn{ border:none;} <span class="btnbox"><input class="btn" type="button" value="按钮"></span> 第二种办法通过滤

chrome自动填表会遮挡input中背景图的问题解决方法

在做某项目登录界面时,发现用户密码框在Chrome自动填充时,input中的背景框会被遮住.网上也搜了一下,没有一个有效的解决方法. 来看csdn的登录界面,也有这个问题. 后来在浏览网页时,无意中发现某网站的登录页面没有这个问题,于是分析了下它的css,照着试了一下,真的完美解决.我做的效果: 原理很简单,下面一个 div,上面放 div(显示图片) + input,把图片div放在input的上面. 图片div的css: .user {background:url("/images/logi