2017-06-17 51 views
1

HTML部分存儲數據到在FORMDATA

<div ng-repeat="tag in tags"> 
     <input type="text" ng-model="user.tags" /> 
    </div> 

角部通過

$scope.tags = [] 
    $scope.addTag = function() { 
     $scope.tags.push({ 
    }) 
} 
var info = new FormData(); 
info.append("tags", $scope.tags); 

,所以我想從所有標籤將數據存儲在標籤中的單個陣列。 我需要@HassanImam通過該數組元素FORMDATA信息

+1

你爲什麼要重複 – Sajeetharan

+0

來顯示數組的每個索引的輸入 –

回答

1

提高對答案

var app = angular.module("app", []); 
 

 
app.controller("myCtrl", function($scope) { 
 
    $scope.tags = []; 
 
     
 
    $scope.hisFunction = function() { 
 
     if($scope.x){ 
 
     $scope.tags.push($scope.x); 
 
     $scope.x = ""; 
 
     } 
 
    } 
 

 
    $scope.myFunction = function() { 
 
     $scope.tagss="[\""; 
 
     var i=0; 
 
     for(i=0;i<$scope.tags.length;i++){ 
 
     $scope.tagss=$scope.tagss+$scope.tags[i]; 
 
     if(i<$scope.tags.length-1) 
 
     $scope.tagss=$scope.tagss+"\",\""; 
 
     } 
 
     $scope.tagss=$scope.tagss+"\"]"; 
 

 
     var info = new FormData(); 
 
     info.append("tags", $scope.tagss); 
 
     for (var pair of info.entries()) { 
 
     console.log(pair[0]+ ', ' + pair[1]); 
 
     } 
 
    } 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app='app' ng-controller="myCtrl"> 
 
    <div ng-repeat="tag in tags track by $index"> 
 
     <input type="text" ng-model="tag" /> 
 
    </div> 
 
     <input type="text" ng-model="x" /> 
 
     <button ng-click="hisFunction()">Add</button> 
 
     <button ng-click="myFunction()">Submut</button> 
 
</div>

我知道這是不是一個適當的解決方案,但嘿它的作品。
我希望它有幫助。

+1

感謝他的工作。 –

+1

很高興我能幫上忙。 –

3

var app = angular.module("app", []); 
 

 
app.controller("myCtrl", function($scope) { 
 
    $scope.tags = []; 
 
     
 
    $scope.myFunction = function() { 
 
     if($scope.x){ 
 
     $scope.tags.push($scope.x); 
 
     $scope.x = ""; 
 
     } 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app='app' ng-controller="myCtrl"> 
 
    <div ng-repeat="tag in tags track by $index"> 
 
     <input type="text" ng-model="tag" /> 
 
    </div> 
 
     <input type="text" ng-model="x" /> 
 
     <button ng-click="myFunction()">Click Me!</button> 
 
</div>

+0

我試過你的代碼,它將變量存儲在數組中,但是當我將它傳遞給formData時,它不再是數組 –

+0

請參閱[this] (https://stackoverflow.com/questions/16104078/appending-array-to-formdata-and-send-via-ajax)爲您的第二部分。 –