使用可选参数POST-method + angularJS发送表单

问题描述 投票:0回答:1

我正在尝试创建一个表单,它发送一个类型为/:id /:选项的url,我在输入表单之前知道id,但是option-attribute是用户从单选按钮中选择的那个。提交表单后,我应该转到页面/:id /:选项。我也在使用angulasJS和我的应用程序。

那么如何使用POST方法发送url?

HTML

<form method="POST">
    <div class="voteOptions" ng-repeat="item in id.data.options">
      <label class="radioButtons">{{item.title}} votes:{{item.votes}}
        <input type="radio" name="option" value={{item.option}}>
        <span class="radioSelector"></span>
      </label>
    </div>
    <input type="submit" id="voteSubmit" value="Vote!">
  </form>

.POST呼叫

app.post('/:id/:option', (req, res) => {
  var poll = polls[req.params.id-1];
  poll.options[req.params.option-1].votes++;
  res.json(poll);
});
javascript html angularjs
1个回答
1
投票

创建控制器并从那里进行Http调用。

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

<form method="POST" ng-submit="submit()">
    <div class="voteOptions" ng-repeat="item in options">
      <label class="radioButtons">{{item.title}} votes:{{item.votes}}
        <input type="radio" name="option" value={{item.option}} ng-model="option.val">
        <span class="radioSelector"></span>
      </label>
    </div>
    <input type="submit" id="voteSubmit" value="Vote!">
  </form>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope,$http) {
    var id = 12345;
	$scope.option = {val:0};
	$scope.options= [{
    	title : 'option-1',
        votes : 5,
        option : 1
    },{
    	title : 'option-2',
        votes : 5,
        option : 2
    }];
    
    $scope.submit = function(){
    	console.log('Url', `<domain>/${id}/${$scope.option.val}`);
        // Generate url and call http post
         // $http.post(url, body, config)
        //  .success(function (data, status, headers, config) {
        //  })
        //     .error(function (data, status, header, config) {
        //     });
    }
});
</script>

</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.