Angularjs 使用ng repeat时默认选中单选按钮
我一直想用ng repeat从屏幕上显示的单选按钮列表中签出一个单选按钮,但我的代码不起作用。这就是我正在做的:Angularjs 使用ng repeat时默认选中单选按钮,angularjs,html,radio-button,Angularjs,Html,Radio Button,我一直想用ng repeat从屏幕上显示的单选按钮列表中签出一个单选按钮,但我的代码不起作用。这就是我正在做的: <div class="clubRole" data-ng-if="club.checked"> <div data-ng-repeat="role in securityGroups.slice(0,1)"> <input type="radio" class="clubRole" data-ng-model="club.rol
<div class="clubRole" data-ng-if="club.checked">
<div data-ng-repeat="role in securityGroups.slice(0,1)">
<input type="radio" class="clubRole" data-ng-model="club.role" data-ng-value="role.securityGroupCode" checked="checked"> {{role.description}}
</div>
<div data-ng-repeat="role in securityGroups.slice(1,securityGroups.length+1)">
<input type="radio" class="clubRole" data-ng-model="club.role" data-ng-value="role.securityGroupCode"> {{role.description}}
</div>
</div>
{{role.description}}
{{role.description}}
代码的目的是检查第一个单选按钮,其他单选按钮不检查。该代码有一个问题:它不工作。但至少它给出了我要做的事情的想法:我希望默认选中其中一个单选按钮,不管它是哪一个。您不需要
checked=“checked”
,我认为如果您将模型设置为其中一个值,angular将自行处理。比如:
club.role = securityGroups.slice(0,1)[0].securityGroupCode;
此外,范围可能会让您在这里绊倒,模型可能必须是
$parent.club.role
单选按钮将检查输入属性的值是否等于单选按钮上应用的模式值
<div ng-repeat="val in ['a','b','c']">
<input
type="radio"
name="val"
data-ng-model="role"
data-ng-value="val"
ng-init="$index==0?(role=val):''"
/>
{{val}}
</div>
{{val}}
Checked=“Checked”在角度上下文中不起作用。您可以在控制器中显式设置单选按钮的值,也可以像我在上面的示例中所做的那样在视图中管理它。但是根据inmput元素的value属性,模式应该相等
例如,如果三个单选按钮上的模态为x,并且每个单选按钮具有不同的值,如a、b和c。那么x必须等于要检查的任何值
你不必担心检查过的问题 HTML:
此处使用小提琴:如果您在列表中使用原语类型,bm1729的答案是正确的,但是如果您在列表中使用对象,请查看以下示例: 第一部分是错误的,因为所选对象与列表项看起来相同,但通过引用它不相同。在这种情况下,==运算符为false
$scope.selected = {c:'red'};
$scope.options = [{c:'red'}, {c:'blue'}, {c:'yellow'}, {c:'green'}];
$scope.options3 = [{c:'red'}, {c:'blue'}, {c:'yellow'}, {c:'green'}];
$scope.selected3 = $scope.options3[0];
但第二个和第三个示例是使用列表项引用,并选中单选按钮。在这种情况下,==运算符为true
$scope.selected = {c:'red'};
$scope.options = [{c:'red'}, {c:'blue'}, {c:'yellow'}, {c:'green'}];
$scope.options3 = [{c:'red'}, {c:'blue'}, {c:'yellow'}, {c:'green'}];
$scope.selected3 = $scope.options3[0];
{{o.displayName}}
你应该使用data ng model=“$parent.club.role”
因为你使用了ng如果@pankajparkar,我相信是ng repeat
创建了它自己的范围,而不是ng-if@Victorng如果
也创建了一个新的范围,那么您需要查看nice and clean+1您的答案只是代码。请用一些解释来改进它。
<md-radio-group ng-model="data.group3">
<md-radio-button value="{{o._id}}" class="md-primary" ng-repeat="o in lstDataRecord" ng-click="updTemplateId(o._id)">
<div flex-gt-sm="50" style="height: 150px; width: 250px;">
<img src="{{PageInfo_PATH}}{{o.imgUrl}}" />
{{o.displayName}}
</div>
</md-radio-button>
<md-radio-button value="active" class="md-primary" [checked]='true'> </md-radio-button>
</md-radio-group>