在我的页面上,我从数据库中动态生成了输入标签。这些字段可能看起来像这样:
<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>
我需要找到一种方法来设置以下键值格式的模型绑定(bind):
$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 双向绑定(bind)。
最佳答案
<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>
Controller :
function ctrl($scope){
$scope.cars = ["Volvo","Saab"];
$scope.customFieldsDictionary = [{
...
}];
}
关于javascript - 如何在 AngularJS 中创建类似模型绑定(bind)的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20093645/