在angularjs中动态创建ng模型

时间:2013-11-21 15:46:42

标签: javascript angularjs model angularjs-ng-repeat

我的属性字段包含字符串数组:

控制器中的

我:

$scope.data = {};

$scope.fields = [
    "new0.name",
    "new0.type",
    "new0.address",
    "new0.city",
    "new0.postalCode"
];

在视图中我有:

  <div class="control-group" ng-repeat="f in fields">
    <label class="control-label">Some field name</label>
    <div class="controls">
      <input type="text" name="{{f}}" ng-model="data.f" class="input-xlarge">
    </div>
  </div>

ng-repeat 中,如何将 f 的值连接到ng-model以获取,例如ng-model =“data.new0 .NAME“?

1 个答案:

答案 0 :(得分:2)

要使用变量引用对象的属性,请使用括号表示法

data[f]