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>
问题解决了。。。。
Angular2 *ngFor把数据显示在多个input中出错解决方法的更多相关文章
- WebView中input file的解决方法
public class MyWb extends Activity { /** Called when the activity is first created. */ WebView web; ...
- Android WebView 不支持 H5 input type="file" 解决方法
最近因为赶项目进度,因此将本来要用原生控件实现的界面,自己做了H5并嵌入webview中.发现点击H5中 标签 不能打开android资源管理器. 通过网络搜索发现是因为 android webvie ...
- thinkphp配置rewrite模式访问时不生效 出现No input file specified解决方法
使用thinkphp配置rewire模式的路径访问网站时, 直接复制官网的.htaccess文件的代码复制过去 1 2 3 4 5 6 <IfModule mod_rewrite.c> ...
- 移动端页面输入法挡住input输入框的解决方法
1,宽高用了百分比或者vw/vh布局的,input输入框的最外层父容器的可用JS动态设置为当前窗口的宽高(防止输入法的弹出令页面变形) 2,最外层父容器用了fixed定位的,不要用top:0;要用bo ...
- thinkphp 5.6以上版本出现No input file specified解决办法
打开thinkphp,出现No input file specified. 解决方法:在工程下的.htaccess文件里, 把RewriteRule ^(.*)$ index.php/$1 [QSA, ...
- PHP icov转码报错解决方法,iconv(): Detected an illegal character in input string
iconv(): Detected an illegal character in input string 错误解决方法 //转码 function iconv_gbk_to_uft8($strin ...
- js 实时监听input中值变化
注意:用到了jquery需要引入jquery.min.js. 需求: 1.每个地方需要分别打分,总分为100; 2.第一个打分总分为40; 3.第二个打分总分为60. 注意:需要判断null.&quo ...
- 实现滑动条与表单中的input中的value交互
最近在写一个考试系统的项目,遇到一些比较有意思的小知识,在这里分享给大家 下面是一个滑动条与input中的value值的交互,即滑动条的颜色会跟随给定input的value值实时变化,虽然表单中的ra ...
- laravel Input Cokkie 的各种方法 超实用!!!
基本输入 Laravel使用一种简单的方式来访问用户提交的信息. 你可以用统一的方式来访问用户提交的信息,而不用为用户提交信息的方式操心. 获取一个用户提交的值 代码如下: $name = Input ...
随机推荐
- 【angular5项目积累总结】自定义管道 OrderBy
import { Injectable, Pipe } from '@angular/core'; @Pipe({ name: 'orderBy' }) @Injectable() export cl ...
- OpenTLD在VS2012和opencv246编译通过
最近看到了TLD的跟踪视频,觉得很有意思,刚好最近在看行人检测所以就打算下载源码玩一玩,因为源码是Linux版本的(原作者写的是C++和MATLAB的混合编程)C++源码可以在我的博客TLD(一种目标 ...
- HTML5--(2)属性选择器+结构性伪类+伪类
一.属性选择器 [att] 匹配所有具有att属性的 [att=val] 匹配所有att属性等于“val”的 [att~=val] 匹配所有att属性包含“val”或者等于“val”的(val必须是一 ...
- C# 学习笔记(一) Winform利用Assembly反射动态创建窗体
1. 添加Reflection //添加对Reflection程序集引用 using System.Reflection; // 引用窗体创建方法CreateForm,传入参数 private voi ...
- ORB-SLAM2实现(kinect V1/ROS)
实验室电脑环境ubuntu14.04和ROS indigo已经装好. 1. 构建工作空间[非常重要的一步] mkdir -p ~/catkin_ws/src cd ~/catkin_ws/ catki ...
- [LeetCode]Remove Duplicates from Sorted Array题解
Remove Duplicates from Sorted Array: Given a sorted array, remove the duplicates in place such that ...
- bash shell学习笔记(一)—— 常用命令
一.基本的bash shell命令 1.默认bash shell 提示符是美元符号($); 2.bash手册 使用man命令来访问存储在Linux系统上的手册页面,如: bogon:~ Mac$ ma ...
- 20个网页设计师应该学习的CSS3经典教程实例
CSS3技术离我们越近,我们也应该学习一些简单的CSS3技术了,而学习最基本的方法就是模仿,以及观看大师作品的案例.收集了20个基础教程,均是涉及到css3应用范围,值得你和我一起共同学习. Smoo ...
- 一行代码解决各种IE的兼容问题
一行代码解决各种IE的兼容问题 在网站开发中不免因为各种兼容问题苦恼,针对兼容问题,其实IE给出了解决方案Google也给出了解决方案百度也应用了这种方案去解决IE的兼容问题 百度源代码如下 < ...
- Java设计模式—组合模式
组合模式是一种常见的设计模式(但我感觉有点复杂)也叫合成模式,有时又叫做部分-整体模式,主要是用来描述部分与整体的关系. 个人理解:组合模式就是将部分组装成整体. 定义如下: 将对象组合成树形结构以表 ...