[英]angularjs ng-repeat with two radio buttons not functioning properly
Hi i tried to create a dynamic form with radio button, after in my result i cannot able to form correctly 嗨,我尝试使用单选按钮创建动态表单,但无法显示正确的表单
My code is 我的代码是
<tr ng-repeat="i in [1,2,3] track by $index">
<td><input type="text" class="form-control"></td>
<td>
<label>
<input ng-model="$parent.reservation.gender" type="radio"
name="gender.$index"
value="male" checked>
Male
</label>
<label>
<input ng-model="$parent.reservation.gender" type="radio"
name="gender.$index"
value="female" checked>
Female
</label>
</td>
<td><input type="text" class="form-control"></td>
</tr>
I my scenario i have to select each row's gender but i cannot able to set parameters to run correctly 我的场景我必须选择每行的性别,但是我无法设置参数才能正确运行
Don't use checked
. 不要使用
checked
。 You should initialize you model with value then it will be checked: 您应该使用值初始化模型,然后对其进行检查:
<tr ng-repeat="i in [1,2,3] track by $index" ng-init="$parent.reservation.gender='female'">
Also I bet you mean name="gender.{{$index}}"
instead of name="gender.$index"
另外,我敢打赌,您是说
name="gender.{{$index}}"
而不是name="gender.$index"
http://plnkr.co/edit/X8YXa5n5gLkjXOTTB7RL?p=preview http://plnkr.co/edit/X8YXa5n5gLkjXOTTB7RL?p=preview
It's not fully clear from you description what "run correctly" means, so I'm going to guess a bit. 从您的描述中还不能完全清楚“正常运行”是什么意思,所以我会猜测一下。
You want to have a reservation
object (that lives in the parent scope of the ng-repeat
) hold the properties of N people. 您想要一个
reservation
对象(位于ng-repeat
的父范围内)来保存N个人的属性。 In other words, if you had a ReservationCtrl
, you want it to have an object of the following form: 换句话说,如果您有一个
ReservationCtrl
,则希望它具有以下形式的对象:
$scope.reservation =
[
{name: "Foo", gender: "male", age: 23},
{name: "Bar", gender: "female", age: 18},
// and so on...
];
So, define a controller: 因此,定义一个控制器:
app.controller("ReservationCtrl", function($scope){
$scope.reservation = [{}]; // array with the an uninitialized person
$scope.addPerson = function(){ $scope.reservation.push({}); }
});
Then in the View: 然后在视图中:
<div ng-repeat="person in reservation">
<input ng-model="person.name" type="text">
<label>Male:
<input ng-model="person.gender" name="gender{{$index}}" type="radio" value="male">
</label>
<label>Female:
<input ng-model="person.gender" name="gender{{$index}}" type="radio" value="female">
</label>
<input ng-model="person.age" type="number">
</div>
<button ng-click="addPerson()">Add another person</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.