简体   繁体   中英

Radio Buttons ng-value, ng-model and ng-click

I have a simple form that renders items and provide radio buttons. Radio buttons are rendered as expected. My goal is to get 'item.chosen' value in the selectRadioButton function. I am getting the item but item.chosen is undefined. What am I doing wrong here?
HTML:

<div class="item" ng-if="ctrl.items" ng-repeat="item in ctrl.items">
    <input type="radio" ng-model="item.chosen"
        ng-value=true ng-click="ctrl.selectRadioButton(item)">
</div>

JS:
 this.selectRadioButton = function(item){ if(item.chosen) { console.log('Got the chosen value'); } }

Here is some sample code of how you should work with radio buttons on AnuglarJS.

Controller:

  angular.module('radioExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.color = {
        name: 'blue'
      };
      $scope.specialValue = {
        "id": "12345",
        "value": "green"
      };
    }]);

HTML:

<form name="myForm" ng-controller="ExampleController">
  <label>
    <input type="radio" ng-model="color.name" value="red">
    Red
  </label><br/>
  <label>
    <input type="radio" ng-model="color.name" ng-value="specialValue">
    Green
  </label><br/>
  <label>
    <input type="radio" ng-model="color.name" value="blue">
    Blue
  </label><br/>
  <tt>color = {{color.name | json}}</tt><br/>
 </form>

Check documentation for more info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM