无法获得角度为

时间:2017-02-04 10:45:31

标签: javascript angularjs

我有这些单选按钮,但我无法从提交中获得价值。我试过这个: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

2 个答案:

答案 0 :(得分: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 :(得分: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>