2017-10-06 54 views
-3

我想通過網絡API插入在angularJS控制器多行值我怎樣才能在表單域發送多行值與angularJS到網頁API

<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發送數組值的列表,網頁API

+0

沒有得到你的問題。你想從web api獲取響應並將其設置到UI或從UI發送數據到web api。 –

回答

0

首先你的HTML是無效的,這就是爲什麼你的NG-重複可能無法工作。您必須在<table></table>中包裝<tr></tr>。而且,爲了通過web API將數據發送到服務器,您應該使用$http服務。更具體地說,如果你需要「創建」東西你可能會使用POST方法。

欲瞭解更多信息,你必須看看$http service documentation

結束你的btnSave()應該使這種操作和使用$http服務。

你的finaly代碼可能看起來像這樣。

模板

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

+0

我要插入多行這樣PARAMS = { 「名」:$ scope.name },但我有孩子PARAMS這樣PARAMS {「電子郵件」:$ scope.email}如何發送父母和孩子響應值API通過angularjs – White

+0

$ scope.newarray = []; PARAMS = { 「ID」:$ scope.id 「名稱列表」:[{ 「名稱」:$ scope.name } ] } angular.forEach($ scope.nameList,函數(響應){ $ scope.newarray.push({name:response.name}); });我需要在webapi中插入nameList數組,如何解決這個問題 – White

+0

使用http POST方法。檢查我的回答鏈接 – Korte