超文本标记语言部分
<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);
所以我想将所有标签中的标签数据存储在一个数组中。我需要将该数组传递给formData element info
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>
改进@Hassaniam的答案
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>