AngularJS - 是否选择了Radio组?

时间:2015-11-24 17:25:53

标签: angularjs

在Angular我有一组收音机? 如何找到至少有一个被选中?

HTML:

<html>

<head>
  <script data-require="angular.js@1.4.6" data-semver="1.4.6" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.min.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body ng-app='jayApp' ng-controller='jayController'>
  <h1>Angular radio group selected ??</h1>


  <input type="radio" name="country" ng-model='country'> India
  <input type="radio" name="country" ng-model='country'> UK
  <input type="radio" name="country" ng-model='country'> Germany
  <input type="radio" name="country" ng-model='country'> France

  <br>
  <br> A country selected ?? : {{country}}

</body>

</html>

Plunnker:http://embed.plnkr.co/cN6xhL/preview

1 个答案:

答案 0 :(得分:2)

您需要为每个value添加input,以便有角色可以锁定,并且如果您只是想知道某些内容发生了变化,请使用ng-change。

<input type="radio" name="country" ng-model='country' value='india' ng-change="change()"> India
<input type="radio" name="country" ng-model='country' value='uk'  ng-change="change()"> UK
<input type="radio" name="country" ng-model='country' value='germany' ng-change="change()"> Germany
<input type="radio" name="country" ng-model='country' value='france' ng-change="change()"> France

http://plnkr.co/edit/GjW8LF?p=preview