我有以下 html 表单:
<form name="queryForm" class="form-inline text-center">
<p class="checkbox-inline onlinecheckbox">
<input type="checkbox" class="onlinecheckbox" name="optionsRadios" id="checkOne" value="one" ng-model="formData.one" ng-true-value="'one'" ng-init="formData.one='one'">
One
</p>
<p class="checkbox-inline onlinecheckbox">
<input type="checkbox" class="onlinecheckbox" name="optionsRadios" id="checkTwo" value="two" ng-model="formData.two" ng-true-value="'two'" ng-init="formData.two='two'">
Two
</p>
<p class="checkbox-inline onlinecheckbox">
<input type="checkbox" class="onlinecheckbox" name="optionsRadios" id="checkThree" value="three" ng-model="formData.three" ng-true-value="'three'" ng-init="formData.three='three'">
Three
</p>
<button type="submit" class="btn btn-fill btn-info" style="margin-top:24px;margin-left:8px" ng-click="queryNumbers()">Refresh</button>
</form>
它在我的页面上显示了 3 个复选框和一个按钮。我想根据用户选择的值构建对我的网络服务的 .post
查询。我为此写了一个函数:
$scope.queryUsers = function(){
var one = $scope.formData.one;
var two = $scope.formData.two;
var three = $scope.formData.three;
var params = [];
if(one){
}
if(two){
}
if(three){
}
//here I want to create a .post query
};
至于现在,我已经准备好我的网络服务,它采用以下格式的 json:
"one":"true" //can be false
"two":"true" //can be false
"three":"true" //can be false
我想创建一个 params
数组并创建后查询 - 如何为每个数字用 true
或 false
值填充此数组基于复选框的值?
最佳答案
您可以使用 $http
,但如果您正在调用 RESTful API,我建议您使用 $resource
(请参阅 documentation ).
查看更多 here 。
就个人而言,我更喜欢在服务内部使用它。
app.factory('Articles',
($resource) => (
$resource(`/path/to/articles/:articleId`)
));
关于javascript - 我如何根据在 Angular.js 页面上单击的复选框构造 .post 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40891298/