如何在AngularJS中创建类似字典的模型绑定
How to create dictionary like model binding in AngularJS
在我的页面上,我从数据库中动态生成了输入标记。这些字段可能是这样的:
<input id='customField-Street' type='text' />
<input id='customField-Height' type='text' />
<input id='customField-IsBlack' type="checkbox" />
<select id='customField-Car'>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</select>
我需要找到一种方法来设置以下键值格式的模型绑定:
$scope.customFieldsDictionary = [{
"Key": "customField-Street",
"Value": "SomeStreet"
}, {
"Key": "customField-Height",
"Value": "125"
}, {
"Key": "customField-IsBlack",
"Value": "true"
}, {
"Key": "customField-Car",
"Value": "volvo"
}];
我需要键值格式,因为我的服务接受该格式的自定义数据。
问题:如何在输入字段和$scope.customFieldsDictionary
字段之间以指定的类似字典的格式设置AngularJS双向绑定。
<div ng-repeat="obj in customFieldsDictionary">
<input ng-model="obj.Value" id='{{obj.Key}}' ng-if="obj.Key ==
'customField-Street' || obj.Key == 'customField-Height'" type='text'/>
<input ng-model="obj.Value" id='{{obj.Key}}' ng-if="obj.Key ==
'customField-IsBlack'" type="checkbox" />
<select ng-model="obj.Value" id='{{obj.Key}}' ng-if="obj.Key ==
'customField-Car'" ng-options="car for car in cars"></select>
</div>
控制器:
function ctrl($scope){
$scope.cars = ["Volvo","Saab"];
$scope.customFieldsDictionary = [{
...
}];
}
相关文章:
- javascript结合了数组和字典
- 在VanillaJS中模拟模型双向数据绑定
- 为集合分配大量的模型弹药
- 如何使用backbone.js从集合中获取模型名称
- python到“;流“;字典处理
- 无法为打字稿字典赋值
- 骨干模型默认值-todos.js示例中不必要的代码
- EmberJS中支持单字母单词模型
- 2个backbone.js集合,具有相同的模型,但排序顺序不同
- 将不在模型中的数据返回到mvc控制器
- 如何在视图模型contet更新更新上调用Jquery函数
- 如何使用Javascript客户端对象模型检索Sharepoint 2010列表项权限
- 我应该如何检查主干.主干.模型更改时查看
- Ext.js从json构建模型关系的问题
- 显示模块模式在Knockout中设置模型的新实例
- 使用导航属性创建Kendo UI网格模型的问题
- Angular,函数在(模型)工厂中返回值
- 如何将本地json数据加载到Extjs数据模型中
- 有没有比在app.js上绑定模块名称更好的方法来动态加载视图模型和视图以显示模态
- 如何在AngularJS中创建类似字典的模型绑定