ng-model没有选择单选按钮

时间:2017-03-30 09:18:17

标签: javascript angularjs

我在ng-repeat中有一个输入类型的无线电。 整个div加载一个弹出窗口,所以我通过parentScope访问控制器属性。

<div ng-repeat="name in parentScope.PatientInfo">
      <input type="radio" id="select-radio-{{$index}}" ng-class="{'radio-btn':true, 'newpaymentmethod1' : true}" 
        ng-model="parentScope.nameObject"  name="object">
</div>

我正在获取parentScope.PatientInfo的值,但我的输入类型广播未默认选中或点击。 我尝试将value and ng-value提供给我的输入类型广播,但都是徒劳的。

1 个答案:

答案 0 :(得分:2)

&#13;
&#13;
var app = angular.module('myApp', []);
app.controller('myController', function($scope) {

  $scope.selected = {
    "name": "",
    "zipCode": "",
    "addressType": "",
    "message": ""
  };
  $scope.items = [{
    "name": "newyork",
    "zipCode": "80166",
    "addressType": "shipToHome",
    "message": "test"
  }, {
    "name": "toronto",
    "zipCode": "80166",
    "addressType": "shipToHome",
    "message": "test"
  }, {
    "name": "california",
    "zipCode": "80166",
    "addressType": "shipToHome",
    "message": "test"
  }];
  $scope.MessageDelivery = function(name) {
    $scope.selected.message = "Will be Delivered to " + name;

  };
  $scope.getItems = function() {
    return $scope.items;
  };

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp">
  <div ng-controller="myController">
    <div>Which one?</div>
    <label class="radio" ng-repeat="item in getItems()">
          <input type="radio" ng-model="selected.name" name="itemOptions" ng-change="MessageDelivery(item.name)" value="{{item.name}}">{{item.name}} - {{item.zipCode}} - {{item.addressType}}
        </label>
    <hr />
    <div>You picked: {{selected.name}} - {{selected.message}} </div>
  </div>
</div>
&#13;
&#13;
&#13;