2017-02-04 51 views
0

我有這些單選按鈕,但我無法從提交時獲得價值。我想這完全一樣的:http://plnkr.co/edit/4lvJclZuh0lryF1nhmdt?p=preview無法獲得角度單選按鈕的價值

HTML:

 <body ng-controller="ExamCtrl"> 
      <form name="myForm" ng-submit="submitForm()"> 
      <label><input type="radio" name="test" ng-model="radioValue" value="1"/> One</label> 
      <label><input type="radio" name="test" ng-model="radioValue" value="2"/> Two</label> 
      <label><input type="radio" name="test" ng-model="radioValue" value="3"/> Three</label> 
      <div>currently selected: {{radioValue}}</div> 
      <button type="submit">Submit</button> 
      </form> 
     </body> 

JS:

$scope.submitForm = function() { 
     console.log('Submitting'); 
     console.log($scope.radioValue); 
    }; 

它記錄 '提交',然後undefined

+0

你試過了你的plnkr嗎? –

+0

你應該在控制器中初始化'$ scope.radioValue;'。 – ProCylon

+0

@NiRmaL然後它只記錄初始化的值。 –

回答

2

它適用於我使用點'。'符號。不知道爲什麼。

HTML

<body ng-controller="ExamCtrl"> 
    <form name="myForm"> 
    <label><input type="radio" name="test" ng-model="radio.value" value="1"/> One</label> 
    <label><input type="radio" name="test" ng-model="radio.value" value="2"/> Two</label> 
    <label><input type="radio" name="test" ng-model="radio.alue" value="3"/> Three</label> 
    <div>currently selected: {{radioValue}}</div> 
    <button ng-click="submitAnswer(radio.value)">Submit</button> 
    </form> 
</body> 

JS

$scope.submitAnswer = function (selected) { 
    // $scope.answer = 'A'; 
    console.log('submitting');   
    console.log(selected); 
}; 
1

您的代碼工作正常:

小提琴:https://jsfiddle.net/2scu8wL8/

var myApp = angular.module('myApp',[]); 
 

 
myApp.controller('MyCtrl',function($scope) { 
 
    $scope.submitForm = function() { 
 
     console.log('Submitting'); 
 
     console.log($scope.radioValue); 
 
    }; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="MyCtrl"> 
 
      <form name="myForm" ng-submit="submitForm()"> 
 
      <label><input type="radio" name="test" ng-model="radioValue" value="1"/> One</label> 
 
      <label><input type="radio" name="test" ng-model="radioValue" value="2"/> Two</label> 
 
      <label><input type="radio" name="test" ng-model="radioValue" value="3"/> Three</label> 
 
      <div>currently selected: {{radioValue}}</div> 
 
      <button type="submit">Submit</button> 
 
      </form> 
 
</div>