NG-模型AngularJS不工作的单选按钮 [英] ng-model not working for radio button in AngularJS

查看:164
本文介绍了NG-模型AngularJS不工作的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我新的角度,我试图获得该用户已采用NG-模式选择的单选按钮的值。但我没有得到在选定的联系人任何输出。

I am new to Angular and I am trying to obtain the value of the radio button that the user has selected using ng-model. But I am not getting any output in "selected contact".

下面是我的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>

下面是我的main.js

Below is my main.js

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

我在做什么错在这里?没能弄清楚!

What am I doing wrong here? Not able to figure out !!!

推荐答案

由于 NG-重复创建其自己的范围,你正在尝试做的是将值赋给从子范围父范围。所以,你可以做

Because ng-repeat creates its own scope and what you are trying to do is assign the value to the parent scope from the child scope. So you can do

<input type="radio" ng-model="$parent.contactname" name="group1" value="{{contact.name}}">

这篇关于NG-模型AngularJS不工作的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆