Javascript AngularJS中绑定ng重复循环的动态ng模型
我有以下字段集合Javascript AngularJS中绑定ng重复循环的动态ng模型,javascript,angularjs,Javascript,Angularjs,我有以下字段集合 $scope.fields = ['name','postcode','phone']; 我需要动态生成与上述字段一样多的输入控件。下面是一个固定版本 <div class="col-sm-3" ng-repeat="user in users"> <div ng-repeat="field in fields"> <input class="form-control" ng-model="user.field" />
$scope.fields = ['name','postcode','phone'];
我需要动态生成与上述字段一样多的输入控件。下面是一个固定版本
<div class="col-sm-3" ng-repeat="user in users">
<div ng-repeat="field in fields">
<input class="form-control" ng-model="user.field" /> <!-- .field isn't resolving -->
</div>
</div>
希望能生成如下内容
<div class="col-sm-3" ng-repeat="user in users">
<div><input class="form-control" ng-model="user.name" /></div>
<div><input class="form-control" ng-model="user.postcode" /></div>
<div><input class="form-control" ng-model="user.phone" /></div>
</div>
有什么想法吗?谢谢 您需要使用来访问变量对象属性:
<div class="col-sm-3" ng-repeat="user in users">
<div ng-repeat="field in fields">
<input class="form-control" ng-model="user[field]" />
</div>
</div>
演示:用户来自哪里<代码>字段在您给出的示例中不是
用户
的属性,因此它不起作用。@DeoElectrix Angular将在需要时创建必要的缺少属性。@dfsq谢谢,我的错误,您每天都会学到一些新东西……;)括号符号就是我要找的!非常感谢。我猜它应该是ng model=“user[{{field}}]”no?@jme11 no,在表达式内部,它实际上是用户
的属性,键为{{field}'
,没有引号用户[{{field}]
,这将是例外。答案中有一个演示,只需自己尝试并检查结果。很酷,之前没有注意到演示,但它是有意义的。