【问题标题】:How can I send multiple row values in form field with angularJS to web api如何使用angularJS将表单字段中的多个行值发送到web api
【发布时间】:2018-03-17 23:21:46
【问题描述】:

我想通过 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) {  
}   

我可以一次添加多个行值 如何将 angularjs 中的数组值列表发送到 web api

【问题讨论】:

  • 没有得到你的问题。您是想从 web api 获取响应并将其设置到 UI 还是从 UI 向 web api 发送数据。

标签: javascript angularjs asp.net-web-api2


【解决方案1】:

首先,您的 html 无效,这就是您的 ng-repeat 可能无法正常工作的原因。您必须将&lt;tr&gt;&lt;/tr&gt; 包裹在&lt;table&gt;&lt;/table&gt; 中。此外,为了通过 web api 向服务器发送数据,您应该使用$http 服务。更具体地说,如果您需要“创建” 一些新的东西,您可以使用 POST 方法。

更多信息请查看$http service documentation

结束您的btnSave() 应该进行此操作并使用$http 服务。

您的最终代码可能看起来像这样。

模板

<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>

控制器

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

  }

这是working sample of your code.

【讨论】:

  • 我必须像这样插入多行 params = { "name": $scope.name } 但我有像这样的子参数 params{"email":$scope.email} 如何发送父和通过 angularjs 的子响应值 api
  • $scope.newarray=[]; params = { "id": $scope.id "nameList": [ { "name": $scope.name } ] } angular.forEach($scope.nameList, function (response) { $scope.newarray.push({名称:response.name }); });我需要在 webapi 中插入 nameList 数组,如何解决这个问题
  • 使用 http POST 方法。检查我的答案中的链接
  • 你好,这不是我用的。
  • 我这样使用 params = JSON.stringify(params);警报(参数); LoadDataService.LoadData(params).then(function (response) {
猜你喜欢
  • 2019-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-22
  • 1970-01-01
  • 2015-10-15
  • 1970-01-01
相关资源
最近更新 更多