[英]Binding using ng-model inside ng-repeat in angularjs
I'm trying to bind a model 'User' to a list of input fields. 我正在尝试将模型“用户”绑定到输入字段列表。 I do not know the fields beforehand, so i've to write a generic code to setup the form based on the fields.
我事先不知道这些字段,因此我必须编写通用代码以根据这些字段设置表单。
<script>
function MyController($scope){
$scope.fields = ['name','password','dob'];
$scope.user1 = {name:"Shahal",password:"secret"}
};
</script>
<div ng-app ng-controller="MyController">
<ul>
<li ng-repeat="field in fields">
<label>{{field}}</label><input type="text" ng-model="user1.{{field}}">
</li>
</ul>
<pre>{{fields}}</pre>
</div>
I'm trying to loop through the fields and show a input field for each fields (available in scope). 我试图遍历这些字段并显示每个字段的输入字段(在范围内可用)。 But the binding is not proper as i'm trying to evaluate an expression inside ng-model.
但是绑定不正确,因为我正在尝试评估ng-model内部的表达式。
Basically i'm trying to show 3 input fields (name,password,dob) with the object user1 attached to the corresponding field. 基本上,我试图显示3个输入字段(名称,密码,标识),并将对象user1附加到相应的字段。
Any help? 有什么帮助吗?
Below will solve your problem 下面将解决您的问题
<script>
function MyController($scope){
$scope.fields = ['name','password','dob'];
$scope.user1 = {name:"Shahal",password:"secret"}
};
</script>
<div ng-app ng-controller="MyController">
<ul>
<li ng-repeat="field in fields">
<label>{{field}}</label><input type="text" ng-model="user1[field]">
</li>
</ul>
<pre>{{fields}}</pre>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.