ng-checked和ng-change单选按钮无法一起使用-angularjs [英] ng-checked and ng-change radio button not work together - angularjs
问题描述
http://plnkr.co/edit/RP9SpO1qGjn5Ua6pZJ3D?p=preview
js
angular.module("sampleapp", []).controller('samplecontroller', function($scope,$rootScope) {
$scope.radii = [
{id:.25, checked:false, name:"1/4 Mile"},
{id:.5, checked:false, name:"1/2 Mile"},
{id:1, checked:false, name:"1 Mile"},
{id:2, checked:true, name:"2 Mile"},
{id:3, checked:false, name:"3 Mile"},
{id:4, checked:false, name:"4 Mile"},
{id:5, checked:false, name:"5 Mile"}
];
$scope.handleRadioClick = function(){
alert();
};
});
和html
and html
<div class="radio">
<label>
<input type="radio" name="radius"
ng-change="handleRadioClick()"
ng-checked="radius.checked">
{{radius.name}}
</label>
</div>
</li>
注意,将根据半径范围结构检查 2 Mile单选输入。为什么ng-change不会触发该函数?
Notice "2 Mile" radio input is checked based on the radii scope structure. Why doesn't the ng-change trigger the function?
如果我添加ng-model,该函数会触发,但是ng-checked无法正常工作。
If I add ng-model, the function triggers but the ng-checked does not work.
推荐答案
这是因为您没有使用 ng-model
:
FORKED PLUNKER
<div class="radio" ng-repeat="radius in radii" id="selectradius-{{radius.id}}">
<label>
<input type="radio" name="radius"
ng-change="handleRadioClick(selectedRadius)"
ng-model="radius.checked">
{{radius.name}}
</label>
</div>
更新:
很抱歉,我没有注意到您希望检查默认的单选按钮,如果是这种情况,那么您采用的方法是错误的。您必须将模型视为一组单选按钮中的非单个部分,但总体上应将它们视为一个值。您不必使用 ng-repeat
的无线电范围变量,而使用另一个 ng-model
作为 selectedRadius
模型。您的输入单选按钮需要有一个值,在这种情况下,我们将使用 ng-value
来确定模型的当前值。
I'm sorry that I didn't notice that you wanted to have a default radio button checked, if that is the case then the approach you're taking is incorrect. You must consider the model as non-individual parts in a group of radio buttons but as a whole, they are supposed to be one value instead. You don't have to use ng-repeat
's radio scope variable instead use another ng-model
as the selectedRadius
model. Your input radio needs to have a value, and in this case we'll use ng-value
to determine the current value of the model.
更新了副本 [ 2014年9月]
JAVASCRIPT
Controller
Controller
$scope.radii = [
{id:.25, name:"1/4 Mile"},
{id:.5, name:"1/2 Mile"},
{id:1, name:"1 Mile"},
{id:2, name:"2 Mile"},
{id:3, name:"3 Mile"},
{id:4, name:"4 Mile"},
{id:5, name:"5 Mile"}
];
// selected value is {id:2, name:"2 Mile"}
$scope.selectedRadius = $scope.radii[3];
HTML
<div class="radio" ng-repeat="radius in radii" id="selectradius-{{radius.id}}">
<label>
<input type="radio" name="radius"
ng-change="handleRadioClick(radius)"
ng-model="selectedRadius"
ng-value="radius">
{{radius.name}}
</label>
</div>
更新的入侵者 [ 2015年1月]
dcz.switcher的问题以下建议重新选择单选按钮时,上述解决方案不会触发 ng-change
事件处理程序。主要问题是 ng模型
是指 ng-repeat
的作用域,而不是控制器的作用域。范围从第二次更改开始。要解决此问题,您可以使用 $ parent
属性。一种替代方法是使用 controllerAs
别名,并使用别名本身来访问控制器的属性。要了解有关AngularJS范围的更多信息,建议您 阅读有关它的更多信息在这里 。
dcz.switcher's problem below suggested that the solution above does not trigger the ng-change
event handler when a radio button is reselected. The main problem was that the ng-model
was referring to the ng-repeat
's scope and not to the controller's scope from the second change. To solve this problem you can use the $parent
property. An alternative would be to use controllerAs
alias and use the alias itself to access your controller's property. To understand more about scopes in AngularJS, I suggest you read more about it in here.
HTML
<div class="radio" ng-repeat="radius in radii" id="selectradius-{{radius.id}}">
<label>
<input type="radio" name="radius"
ng-change="handleRadioClick($parent.selectedRadius)"
ng-model="$parent.selectedRadius"
ng-value="radius">
{{radius.name}}
</label>
</div>
这篇关于ng-checked和ng-change单选按钮无法一起使用-angularjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!