javascript - 如何在 AngularJS 中创建类似模型绑定(bind)的字典

标签 javascript wcf angularjs dictionary model-binding

在我的页面上,我从数据库中动态生成了输入标签。这些字段可能看起来像这样:

<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/

相关文章:

javascript - 过滤数组递归

javascript - 将用户名存储在从登录到服务器的数组中

javascript - RadioButtonList SelectedIndexChange 即使在弹出表单上

javascript - 使用 jQuery 计算可见的 div

c# - 使用 jQuery 使用 WCF 服务

javascript - Google Places API + Angular JS + PHP 数据未返回

wcf - 使用log4net或NLog的WCF日志记录/跟踪和事件ID传播

c# - WCF REST 服务的 Azure 缓存间歇性响应时间

javascript - AngularJS 服务单例?

javascript - 如何在Angularjs中的字符串插值中获得换行符