5

我正在做一个有角度的项目,我需要根据一系列问题创建一个表单。我想ng-model为数组中的每个问题创建。

所以我想出了类似下面的东西,但它不起作用。

<div class="form-group" data-ng-repeat="question in questions">
    <label for="{{question.label}}" class="col-md-2 control-label">
        {{question.label}}:
    </label>
    <div class="col-md-10">
        <input type="text" 
               class="form-control" 
               name="{{question.label}}" 
               data-ng-model={{question.label}}
               required />
        <span class="error"
              data-ng-show="formQuickView.{{question.label}}.$error.required">
            Required!
        </span>
    </div>
</div>                   

有人可以帮我解决这个问题吗?
提前致谢。

4

2 回答 2

7
formQuickView[question.label].$error.required

这是常规的 JavaScript 语法。您想使用formQuickView定义的名称访问 的属性question.label

更新

不知何故,我错过了重点,ng-model表达。基本上你在这里做同样的事情。您有两种选择(技术上只有一种):

  • 将对象添加到您的范围,例如questions,然后使用questions[question.label].
  • 当你有一个表格,然后给它一个名字,一个对象就会自动添加。eg<form name="questions" ....和上面一样。
于 2014-01-11T10:56:36.307 回答
4

ng-model不适用于{{}}它将传递给它的字符串视为引用范围属性的表达式。

我不确定我是否正确理解了您的代码。在您的代码中,我认为data-ng-model="question.label"应该可以。

如果要引用字段中指定的动态label字段。用你的 ng-model 试试这个:

<input type="text" ng-model="question[question.label]"/>

演示

于 2014-01-11T11:04:12.957 回答