1

HTML part

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

Angular part

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

so i want to store the data from all tag in tags in a single array. I need to pass that array to formData element info

2
  • 1
    why do you have ng repeat Commented Jun 17, 2017 at 7:17
  • to show inputs for each index of the array Commented Jun 17, 2017 at 8:01

2 Answers 2

2

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>

Sign up to request clarification or add additional context in comments.

2 Comments

I tried your code, it stores the variables in an array but when i pass it to formData its no longer an array
Please refer this for your second part.
1

Improving on the answer by @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>

I know this is not a proper solution, but hey it works.
I hope it helps.

1 Comment

glad i could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.