简体   繁体   English

如何使用angularJS将表单字段中的多行值发送到web api

[英]How can I send multiple row values in form field with angularJS to web api

I want to insert the multiple row values in angularJS controller via web api我想通过 web api 在 angularJS 控制器中插入多行值

<tr ng-repeat="rt in xxtt">

      <td>
    <input type="text" class="form-control" ng-model="rt .name" required />
     </td>
    <td>`enter code here`
     <input type="text" class="form-control" ng-model="rt .email" required />
     </td>
    </tr>
    <button class="btn" ng-click="btnSave()">
    Save
    </button>

$scope.newarray=[];
params = {
            "id": $scope.id

            "nameList": [
                    {
                        "name": $scope.name

                    }
            ]
        }     
angular.forEach($scope.nameList, function (response) {
                    $scope.newarray.push({ name: response.name });
                });
params = JSON.stringify(params);        
        alert(params);
        LoadSvc.LoadData(params).then(function (response) {  
}   

I can add multiple row values at a time How can i send list of array values in angularjs to web api我可以一次添加多个行值如何将 angularjs 中的数组值列表发送到 web api

First of all your html is invalid that's why your ng-repeat might not be working.首先,您的 html 无效,这就是您的 ng-repeat 可能无法正常工作的原因。 You have to wrap <tr></tr> in <table></table> .您必须将<tr></tr>包裹在<table></table> Moreover, in order to send data to server via a web api you should use $http service.此外,为了通过 web api 将数据发送到服务器,您应该使用$http服务。 More specifically if you need to "create" something new you might use POST method.更具体地说,如果您需要“创建”新的东西,您可能会使用 POST 方法。

For more information you have to take a look at $http service documentation .有关更多信息,您必须查看$http 服务文档

To conclude your btnSave() should make this operation and use $http service.总而言之,您的btnSave()应该进行此操作并使用$http服务。

Your finaly code might look something like this.您的最终代码可能如下所示。

Template模板

<div ng-app="app" ng-controller="AppController">
  <table>
    <tr ng-repeat="rt in xxtt">
      <td>
        <input type="text" class="form-control" ng-model="rt.name" required />
      </td>
      <td>`enter code here`
        <input type="text" class="form-control" ng-model="rt.email" required />
      </td>
    </tr>
  </table>
  <button class="btn" ng-click="btnSave()">
    Save
  </button>
</div>

Controller控制器

 $scope.btnSave = function() {

            /** You have to use your endpoint here **/
      $http.post('some-endpoint.com', $scope.xxtt).then(function(response) {
                    alert("I've posted data successfully");
            },function() {
          alert("I've failed to post data");
      });

  }

Here's a working sample of your code.是您的代码工作示例。

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

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