【问题标题】:ng-model not working for radio button in AngularJSng-model 不适用于 AngularJS 中的单选按钮
【发布时间】:2013-09-06 19:13:27
【问题描述】:

我是 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>

下面是我的 main.js

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

我在这里做错了什么?想不通!!!

【问题讨论】:

    标签: javascript angularjs angularjs-scope


    【解决方案1】:

    因为ng-repeat 创建了自己的范围,而您要做的是将值从子范围分配给父范围。所以你可以这样做

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

    【讨论】:

    • 我有一个类似于上面的复选框功能(使用 ng-repeat,来自 main.js 中 scope.objects 的值)。 {{item.name}} 我再次陷入让用户检查的所有值...简而言之,如果我们用复选框替换上面的单选按钮...我应该如何进行。也在努力寻找解决方案
    • @Shrey 对于复选框,您需要将单个模型绑定到每个框,这与单选按钮组不同,因为单选按钮组只允许选择一个。您可以执行(假设您通过联系人循环循环)&lt;input type="checkbox" ng-model="contact.checked" name="tier_class"/&gt;,因此在选中框时,每个联系人将分配一个名为checked的字段。 span>
    • 您也可以使用ng-value="contact.name",而不是插入名称,如the docs 中所述。您实际上不需要在这里使用ng-value,因为您使用字符串作为模型(因为value 属性仅适用于字符串),但是如果您想为模型使用任何其他数据类型(布尔值,对象,...)您将需要使用ng-value
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      var app = angular.module('myApp', []);
      app.controller('formCtrl', function($scope) {	
      	
      	$scope.devices = [{
      		nameD: "Device 1"
      		}, {
      		nameD: "Device 2"
      		}, {
      		nameD: "Device 3"
      		}, {
      		nameD: "Device 4"
      	}];
      	
      });
      <!DOCTYPE html>
      <html>
      <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
      <script src="app-controller-r.js"></script>
      <body>
      <div ng-app="myApp" ng-controller="formCtrl">
      <form>
      <ul>
          <li ng-repeat="device in devices">  
      	<label>{{device.nameD}}	
              <input type="radio" ng-model="$parent.deviceType" value="{{device.nameD}}" />       
      	</label>
          </li>
      </ul>
      <button>Submit</button>
      </form>
      {{deviceType}}
       </div>
      </body>
      </html>

      【讨论】:

        【解决方案4】:

        如果您多次使用该指令并且您正在使用带有标签的 forid ... 请确保使用对作用域的 $id 的引用

        <li ng-repeat='add_type in types'>
            <input id="addtester_{{ add_type.k }}_{{ $parent.$id }}" type="radio" ng-model="$parent.addType" ng-value='add_type.k' class="tb-form__input--custom" /
            <label for="addtester_{{ add_type.k }}_{{ $parent.$id }}">{{ add_type.v }}</label>
        </li>
        

        否则,您将成为共享相同输入的指令,并使其看起来好像不起作用。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-03-08
          • 2016-10-13
          • 1970-01-01
          • 2017-01-31
          • 2015-10-13
          • 2016-01-30
          • 2017-05-01
          • 1970-01-01
          相关资源
          最近更新 更多