AngularJS值从无线电到$ http.post

时间:2016-07-03 13:15:27

标签: javascript angularjs post radio-button

我想问你如何从收音机中获取价值并使用AngularJS发送$ http.post。这是一些例子

HTML

  <input name="group1" type="radio" id="test1" value="4"/>
  <label for="test1">Four paintings</label>

  <input name="group1" type="radio" id="test2" value="6" />
  <label for="test2">Six paintings</label>

  <button ng-click="submit()">Submit</button>

一个单选按钮的值为4,另一个为6.这些数字我想发送给Angular然后再发送给DB

$scope.submit = function(){
  if ( radioValue == 4 ) {
     $http.post(apiURL, {
        numberOfpaintings: ??, // radioValue
        ...      
       });
   } else if( radioValue == 6 ) {
     $http.post(apiURL, {
        numberOfpaintings: ??, // radioValue
        ...
       });
   }
}

'radioValue'只是一些组合值,应该以某种方式存储来自radioButtons的值。 谢谢!

1 个答案:

答案 0 :(得分:2)

试试这个

Plunker:http://plnkr.co/edit/b3AAr9XlVcS0IW962tUT

在剧本中:

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

app.controller('MainCtrl', function($scope,$http) {
  $scope.paintings = 4;

  $scope.submit = function(){

  alert($scope.paintings);

 $http.post(apiURL, {
    numberOfpaintings: $scope.paintings
   });
 }
});

in html

<body ng-controller="MainCtrl">

<input name="group1" ng-model="paintings" type="radio" id="test1" value="4"/>
<label for="test1">Four paintings</label>

<input name="group1"  ng-model="paintings" type="radio" id="test2" value="6" />
 <label for="test2">Six paintings</label>

<button ng-click="submit()">Submit</button>


</body>