将来自这些输入的数据存储在数组中以传递 formdata
Store data from these inputs in an array to pass in formdata
HTML 部分
<div ng-repeat="tag in tags">
<input type="text" ng-model="user.tags" />
</div>
Angular部分
$scope.tags = []
$scope.addTag = function() {
$scope.tags.push({
})
}
var info = new FormData();
info.append("tags", $scope.tags);
所以我想将标签中所有标签的数据存储在一个数组中。
我需要将该数组传递给 formData 元素 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>
改进@HassanImam 的回答
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>
我知道这不是一个合适的解决方案,但它确实有效。
希望对你有帮助。
HTML 部分
<div ng-repeat="tag in tags">
<input type="text" ng-model="user.tags" />
</div>
Angular部分
$scope.tags = []
$scope.addTag = function() {
$scope.tags.push({
})
}
var info = new FormData();
info.append("tags", $scope.tags);
所以我想将标签中所有标签的数据存储在一个数组中。 我需要将该数组传递给 formData 元素 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>
改进@HassanImam 的回答
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>
我知道这不是一个合适的解决方案,但它确实有效。
希望对你有帮助。