将来自这些输入的数据存储在数组中以传递formdata

时间:2017-06-17 07:17:02

标签: html angularjs multipartform-data

HTML部分

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>build-a</id>
                    <configuration>
                        <excludes>
                            <exclude>**/Test.*</exclude>
                        </excludes>
                        <includes>
                            <include>**/Test1.*</include>
                        </includes>
                        <archive>
                            <manifest>
                                <mainClass>com.example.MyMain</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <finalName>test1.jar</finalName>
                    </configuration>
                    <phase>package</phase>
                    <goals>
                        <goal>assembly</goal>
                    </goals>
                </execution>
                <execution>
                    <id>build-b</id>
                    <configuration>
                        <excludes>
                            <exclude>**/Test.*</exclude>
                        </excludes>
                        <includes>
                            <include>**/Test2.*</include>
                        </includes>
                        <archive>
                            <manifest>
                                <mainClass>com.example.MyMain</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <finalName>test2.jar</finalName>
                    </configuration>
                    <phase>package</phase>
                    <goals>
                        <goal>assembly</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

角度部分

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

所以我想将标签中所有标签的数据存储在一个数组中。 我需要将该数组传递给formData元素信息

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

改善@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>

我知道这不是一个合适的解决方案,但嘿它有效 我希望它有所帮助。