ng-model不适用于AngularJS中的单选按钮

Shr*_*vam 64 javascript angularjs angularjs-scope

我是Angular的新手,我正在尝试获取用户使用ng-model选择的单选按钮的值.但我没有在"选定的联系人"中获得任何输出.

这是我的HTML

<!doctype html>
<html ng-app>
  <head>
    <script src="http://code.angularjs.org/1.2.0rc1/angular.min.js"></script>
    <script src="script.js"></script>
  </head>
  <body>
    <form name="myForm" ng-controller="Ctrl">
      <table border="0">
                <th>Contact Type</th>
                <tr ng-repeat="contact in contacttype"><td>
                <input type="radio" ng-model="contactname" name="group1" value="{{contact.name}}">{{contact.name}}                  
                </td>               
            </td></tr></table>
      <tt>selected contact = {{contactname}}</tt><br/>
     </form>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

下面是我的main.js

  function Ctrl($scope) {
  $scope.contacttype = [ 
                          {name: 'Both' }, 
                          {name: 'User'},
                          {name: 'Admin'}
                          ];
}
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?无法搞清楚!!!

zs2*_*020 109

因为ng-repeat创建自己的作用域以及您要执行的操作是将值从子作用域分配给父作用域.所以你可以做到

<input type="radio" ng-model="$parent.contactname" name="group1" value="{{contact.name}}">
Run Code Online (Sandbox Code Playgroud)


Jos*_*h C 5

所以我遇到了同样的问题,使用带有ng-model的$ parent似乎没有起作用.我的问题是我有一组复选框,但每个复选框使用相同的名称属性.它最终隐藏了最后一组中的默认选中单选按钮.

确保为每个不同的组使用不同的名称属性.