简体   繁体   English

Ng-重复与不同组的单选按钮

[英]Ng-repeat with different group of radio buttons

I created a group of radio buttons and it didn't assign properly for each group. 我创建了一组单选按钮,并没有为每个组正确分配。 The button is not working when I click one of the button from one of the radio button group. 当我从其中一个单选按钮组中单击其中一个按钮时,该按钮不起作用。

Senario 1 Senario 1

<div ng-repeat="m in meals">
  <label class="linked-radio"><span class="radio primary"><span></span>
    </span>{{ m.name }}
    <input type="radio" id="{{ m.id }}" ng-model="mealselected" ng-checked="isMealSelected(m.id);"/>
  </label>
</div>

The radio button is not check when click 单击时不检查单选按钮

Senario 2 Senario 2

<div ng-repeat="m in meals">
  <input type="radio" id="{{ m.id }}" ng-model="mealselected"/>
    <label class="linked-radio">
        <span class="radio primary">
            <span></span>
        </span>{{ m.name }}
  </label>
</div>

The radio button is checked but only the first one in each every group of radio buttons. 选中单选按钮,但每组单选按钮只检查第一个单选按钮。

radiobutton is grouped by name property, in the below code snippet, I just make 2 radiobutton groups. radiobuttonname属性分组,在下面的代码片段中,我只创建了2个radiobutton组。 And for label , you should use for property and bind it with the relevant radiobutton's id. 而对于label ,你应该使用for财产,并与相关的单选按钮的ID绑定。

 angular.module("app", []) .controller("myCtrl", function($scope) { $scope.mealselected = false; $scope.meals = [ { id: 1, name: 'meal1' }, { id: 2, name: 'meal2' }, { id: 3, name: 'meal3' } ]; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="myCtrl"> <h1>Group1</h1> <div ng-repeat="m in meals"> <label class="linked-radio" for="{{ m.id }}"><span class="radio primary"><span></span> </span>{{ m.name }} <input type="radio" id="{{ m.id }}" name="group1" ng-model="mealselected" /> </label> </div> <h1>Group2</h1> <div ng-repeat="m in meals"> <input type="radio" id="{{ m.id }} + 2" name="group2" ng-model="mealselected" /> <label class="linked-radio" for="{{ m.id }} + 2"> <span class="radio primary"> <span></span> </span>{{ m.name }} </label> </div> </div> 

Use for in label for标签

Code : 代码:

<div ng-repeat="m in meals">
  <input type="radio" id="{{ m.id }}" name="radio_meal" ng-model="mealselected" value="{{m.id}}"/>
    <label for="{{ m.id }}" class="linked-radio">
        <span class="radio primary">
            <span></span>
        </span>{{ m.name }}
  </label>
</div>

You should use ng-repeat in <label> tag instead of <div> tag. 您应该在<label>标签中使用ng-repeat而不是<div>标签。 And use ng-chage instead of ng-checked . 并使用ng-chage而不是ng-checked Like this: 像这样:

 <div> <label class="linked-radio" ng-repeat="m in meals" > {{m.name}} <input type="radio" ng-model="$parent.mealselected" ng-value="m.id" ng-change="$parent.isMealSelected(m.id)"/> </label> </div> 

$parent is used to access parent scope from child $parent用于从child访问父范围

DEMO DEMO

 var myApp = angular.module('myApp',[]); myApp.controller('MyCtrl', function($scope) { $scope.meals = [ { id: 1, name: 'first' }, { id: 2, name: 'second' }, { id: 3, name: 'third' } ]; $scope.mealselected = ''; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="MyCtrl"> <div ng-repeat="m in meals"> <label class="linked-radio" for="{{ m.id }}">{{ m.name }} <input type="radio" id="{{ m.id }}" name="meal" ng-model="mealselected"/> </label> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM