摘要:首先,有一个这样的需求,就是说,我点击添加,会动态出现需要输入的输入框。我们需要定义一个对象,类似这种,
{spc:{},spctions:[]}
意思是spc对应的是一个对象,spctions对应的是一个列表。要实现动态插入的效果,我们只需要往spctions这个列表中插入一个空对应即可{},就是那么的简单。
最终需要插入的是两张表,相当于一对多的关系。这点要明白。当然 这里我们重点讲解 动态插入的原理和angular如何实现。
js代码
//增加规格选项行 ???$scope.addTableRow=function(){ ???????$scope.entity.specificationOptionList.push({}); ???}
html代码
<tbody> ?????????????????????<tr ng-repeat="pojo in entity.specificationOptionList"> ??????????????????????????????????????????????????????<td> ???????????????????????????????<input ?class="form-control" placeholder="规格选项" ng-model="pojo.optionName"> ???????????????????????????</td> ???????????????????????????<td> ???????????????????????????????<input ?class="form-control" placeholder="排序" ng-model="pojo.orders"> ???????????????????????????</td> ???????????????????????????<td> ???????????????????????????????<button type="button" class="btn btn-default" title="删除" ng-click="deleTableRow($index)" ><i class="fa fa-trash-o"></i> 删除</button> ???????????????????????????</td> ?????????????????????</tr> ???????????????????</tbody>
说一下需要注意的地方。这样写肯定不行,因为会报错
entity.specificationOptionList ???未定义,所以 我们要初始化一下。最好在点击新增的按钮上初始化,因为。。。(自己试下就知道了)
在按钮上 添加 这行 ?即可
ng-click="entity={specificationOptionList:[]}"
删除的话,原理是传一个数组坐标,在数组中删除。获取遍历list的坐标是 ?$index ?获取
ok ?超级简单。 不懂留言 ???不得不说 ?双向绑定爽死 要是用jquery ?搞死你 ?框架的魅力
angularJs实现动态增加输入框
原文地址:https://www.cnblogs.com/coder-lzh/p/9094910.html