使用{{expression}}动态创建ng模型不起作用?

lov*_*eNZ 5 javascript angularjs angularjs-model

我正在开展一个角度项目,我需要根据一系列问题创建一个表单.我想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>                   
Run Code Online (Sandbox Code Playgroud)

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

a b*_*ver 7

formQuickView[question.label].$error.required
Run Code Online (Sandbox Code Playgroud)

这是常规的JavaScript语法.您想要访问formQuickView具有由其定义的名称的属性question.label.

更新

不知怎的,我错过了主要观点,ng-model表达方式.基本上你在这里做同样的事情.你有两个选择(技术上只有一个):

  • 比如在对象中添加一个对象questions,然后使用questions[question.label].
  • 如果您有表单,请为其命名,并自动添加对象.例如<form name="questions" ....和上面一样.