javascript - 根据angularjs中的数组数据动态创建表单?
问题描述
我从其余 API 数据构造了一个数组,如下所示
arr = [ {'model1': 'value1', 'type': 'text'},
{'model2': 'true', 'type': 'radio'},
{'model3': 'value3', 'type': 'text'},
];
在这里,我如何为例如第一个元素ng-model
名称model1
及其value
asvalue1
及其类型生成动态形式text/radio
解决方案
首先修复您的数组项属性的名称,以便它们的名称中不包含索引,因为这违背了目的。将数组更改为
arr = [
{'model': 'value1', 'type': 'text'},
{'model': 'true', 'type': 'radio'},
{'model': 'value3', 'type': 'text'},
];
您需要遍历您的数组以生成这样的表单
<form form="arrForm" ng-submit="arrForm.submit()">
<section ng-repeat="value in arr>
<label for="value-{{$index}}-model">Model {{$index}}</label>
<input ng-model="value.model" name="value-{{$index}}-model">
<section>
<label for="value-{{$index}}-type">Type {{$index}} </label>
<input ng-repeat="type in possibleTypes" type="radio" ng-model="value.type" ng-value="type" name="value-{{$index}}-type">
</section>
</section>
<input type="submit" value="Save All" ng-disabled="arrForm.$invalid">
</form>
但是,请考虑使用 AngularJS 表单库。FormlyJS是最好的。
推荐阅读
- jhipster - 如何在 Jhipster 中定义和对象集合?
- excel - 如何使用 Excel 作为数据源并在 UIPath 中执行 SQL 查询
- r - 绘制具有不同长度的 2 个变量以显示比例,而不是绝对数字
- docker - 如何在 Dockerfile 中卸载 docker 卷?
- c# - Azure 功能:在 MAIL FROM [DB6P189CA0021.EURP189.PROD.OUTLOOK.COM] 期间,客户端未经过身份验证以发送匿名邮件
- python - Django 通道:连接到套接字
- nested - TYPO3:带有嵌套 For 循环的 If 语句的内联表示法
- node.js - nodejs base64到blob的转换
- gitlab - 如何在 gitlab 项目页面中隐藏冗余(auto devops kubernetes CI/CD)按钮
- python - 如何在 django admin 的非只读字段中突出显示一些单词?