【问题标题】:angularjs radio button not checkedangularjs单选按钮未选中
【发布时间】:2018-05-10 14:46:17
【问题描述】:

我在ng-repeat 功能中有一个单选按钮。因此,它将在每个步骤上创建单选按钮。但是对于某些步骤,单选按钮会被选中,而对于某些单选按钮,即使我们有来自后端的数据,也不会被选中。

问题:对于某些步骤,即使单选按钮具有 ng-modal 值,也不会检查它

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="">

<div class="temp">
 <label class="radio">
<input type="radio" ng-model="info.Unit" value="1"  name="optradio-00">unit1</label>
 <label class="radio"><input type="radio"  ng-model="info.Unit" value="0"  name="optradio-00"> unit2</label>
</div>  
<div class="temp">
 <label class="radio"><input type="radio"  ng-model="info.Unit" value="1"  name="optradio-01">unit1</label>
 <label class="radio"><input type="radio"  ng-model="info.Unit" value="0"  name="optradio-01">unit2</label>
</div>  

</div>

</body>
</html>

radio image with value printed on top

【问题讨论】:

  • 这是因为您使用的是相同的 'ng-model',它具有重复的 'value'。
  • 如果你想将它们分开,每个收音机必须有一个不同的'ng-model'名称,并使用那些'ng-model'来制作'info.Unit'。

标签: javascript jquery angularjs radio-button


【解决方案1】:

您可以使用以下概念在ng-change 上触发事件

<div ng-controller="MyCtrl">
<input type="radio" ng-model="value" value="radiobutton1" ng-change='newValue(value)'>
<input type="radio" ng-model="value" value="radiobutton2" ng-change='newValue(value)'>
<input type="radio" ng-model="value" value="radiobutton3" ng-change='newValue(value)'>    
<hr> 
</div>

在 app.js 中编写代码

var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
    $scope.value= 'radiobutton1';

    $scope.newValue = function(value) {
       console.log(value);
    }
}

【讨论】:

  • 我可以从后端获取这个值 {{i.Unit}},但是单选按钮没有被选中
  • 请看附图以获得更清晰的信息
  • 请与虚拟数据分享 jsfiddle/plnkr 上的 Demo 示例,以便我可以轻松地帮助您..
  • 嗨桑托什,我已经编辑了可重现的问题。我需要从后端接收值的相同 ng-modal 变量。请检查..
  • 为什么例如这可以完美运行,但当 type='radio' 时却不行?
猜你喜欢
  • 2017-06-21
  • 2013-09-02
  • 1970-01-01
  • 1970-01-01
  • 2012-09-17
  • 1970-01-01
  • 2016-01-23
  • 2017-05-05
  • 2015-05-07
相关资源
最近更新 更多