【问题标题】:How to create dictionary like model binding in AngularJS如何在 AngularJS 中创建类似于模型绑定的字典
【发布时间】:2013-11-20 10:41:17
【问题描述】:

在我的页面上,我从数据库中动态生成了输入标签。这些字段可能如下所示:

<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 的双向绑定。

【问题讨论】:

  • 你从 C# 发送字典了吗?

标签: javascript wcf angularjs dictionary model-binding


【解决方案1】:
<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 = [{ 
        ...
    }];
}

【讨论】:

  • 如果它是来自 c# 的字典,它会以同样的方式工作吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-04
  • 2016-05-07
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
相关资源
最近更新 更多