简体   繁体   English

如何基于Angular.js在页面上单击的复选框构造.post查询?

[英]how can I construct .post query based on checkboxes clicked on page with Angular.js?

I have the following html form: 我有以下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>

and it shows 3 checkboxes on my page and a button. 它在我的页面上显示3个复选框和一个按钮。 I want to construct a .post query to my webservice based on the values user selects. 我想基于用户选择的值构建对我的Web服务的.post查询。 I wrote a function for that: 我为此写了一个函数:

$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
};

As for now, I have my webservice ready, it takes the json in the following format: 到目前为止,我已经准备好了我的Web服务,它采用以下格式的json:

"one":"true" //can be false
"two":"true" //can be false
"three":"true" //can be false

I want to create a params array and create post query - how can I fill this array with true or false values for each number based on the checkboxs' values? 我想创建一个params数组并创建后查询-如何根据复选框的值用每个数字的truefalse值填充此数组?

official doc https://docs.angularjs.org/api/ng/service/ $http 官方文档https://docs.angularjs.org/api/ng/service/ $ http

Working plunkr : http://plnkr.co/edit/D7AV2DavhvNny9rppy4r?p=preview 工作的plunkr: http ://plnkr.co/edit/D7AV2DavhvNny9rppy4r?p=preview

You have to do inject '$http' in your controller depence : 您必须在控制器依赖区中注入“ $ http”:

myApp.controller('Ctrl1', ['$scope','$http',  function($scope,$http) {

then : JS 然后:JS

$scope.queryUsers = function() {
    console.clear();
    var one = $scope.formData.one;
    var two = $scope.formData.two;
    var three = $scope.formData.three;

    //here I want to create a .post query

    $http({
      method: 'POST',
      url: 'path/to/service', // put your url her
      data: {
        "one": one,
        "two": two,
        "three": three
      }

    }).then(function successCallback(response) {
      // success 
    }, function errorCallback(response) {
      // called asynchronously if an error occurs
      // or server returns response with an error status.
    });

  };

html : 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-init="formData.one=false">
        One
    </p>
    <p class="checkbox-inline onlinecheckbox">
        <input type="checkbox" class="onlinecheckbox" name="optionsRadios" id="checkTwo" value="two" ng-model="formData.two" ng-init="formData.two=false">
        Two
    </p>
    <p class="checkbox-inline onlinecheckbox">
        <input type="checkbox" class="onlinecheckbox" name="optionsRadios" id="checkThree" value="three" ng-model="formData.three" ng-init="formData.three=false">
        Three
    </p>
    <button type="submit" class="btn btn-fill btn-info" style="margin-top:24px;margin-left:8px" ng-click="queryUsers()">Refresh</button>
</form>

1)You need to create an associative array,something like this: 1)您需要创建一个关联数组,如下所示:

 var app=angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.queryNumbers = function(){ var one = $scope.formData.one; var two = $scope.formData.two; var three = $scope.formData.three; var params = {}; params["one"]=one!='one'; params["two"]=two!='two'; params["three"]=three!='three'; console.log(params); }; }) 
 .done-true { text-decoration: line-through; color: grey; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script> <div ng-app="myApp"> <div ng-controller="myCtrl"> <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> </div> </div> 

2) Don't forget to send data to server using JSON.stringify(object) ; 2)不要忘记使用JSON.stringify(object)将数据发送到服务器;

JSON.stringify function turns a Javascript object into JSON text and stores it in a string. JSON.stringify函数将Javascript对象转换为JSON文本并将其存储在字符串中。

In your case you need to use something like this: 在您的情况下,您需要使用以下内容:

$http({
  method: 'POST',
  url: 'you_method_url',
  data:JSON.stringify(params)
}).then(..);

You can use $http , but if you are calling a RESTful API, I would suggest you to use $resource (see documentation ). 您可以使用$http ,但是如果您正在调用RESTful API,我建议您使用$resource (请参阅文档 )。

See more here . 在这里查看更多。

Personally, I prefer to use it inside of a service. 就个人而言,我更喜欢在服务内部使用它。

app.factory('Articles',
  ($resource) => (
    $resource(`/path/to/articles/:articleId`)
  ));

I would try to make things a bit more generic. 我会尝试使事情更加通用。

First at all is bad to have the same name attr for multiple input elements. 首先,对于多个输入元素使用相同的name attr完全是不好的。

<form name="queryForm" class="form-inline text-center">
  <p class="checkbox-inline onlinecheckbox">
    <input type="checkbox" class="onlinecheckbox" name="one" 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="two" 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="three" 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>

Then you can access the form by his name in the angular scope and play with the inputs inside without actally know the names! 然后,您可以在角度范围内按他的name访问表单,并在不实际知道名字的情况下使用内部输入!

var form = $scope.queryForm;
var params = [];

for (let name in form) {
  if (form.hasOwnProperty(name) && typeof form[name] === 'object' && form[name].hasOwnProperty('$modelValue')) {
    // build the array as you want here
    params.push($scope.formData[name]);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM