我正在使用AngularJS动态表单。根据我的用户ID值,我使用ng-repeat生成了具有相同型号名称的多个表单字段。由于此ng-repeat,我无法获得此输入模型值。在这个例子中,我使用静态用户ID数据。
<div ng-app="myApp">
<div ng-controller="myCtrl">
<form role="form" name='userForm' novalidate>
<div class="container">
<div class="row" ng-repeat="myid in userid">
<div class="form-group">
<div class="col-md-3">
<label>ID</label>
<input ng-model="myid" id="myid" name="myid" placeholder="Enter bugid" type="text" required readonly disabled>
</div>
<div class="col-md-3">
<label>Comments</label>
<textarea ng-model="manualComment" id="textarea1" rows="1" required></textarea>
</div>
<div class="col-md-3 ">
<label>Gender</label>
<select ng-model="gender" name="select2" required>
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</div>
</div>
</div>
</div>
<div class="buttonContainer text-center btn-container">
<br>
<button ng-disabled="userForm.$invalid" type="button" id="adduser" ng-click="adduser">Add user</button>
<button type="button" class="btn button--default btn--small pull-center">Close</button>
</div>
</form>
</div>
我的js代码
var myApp = angular.module('myApp', []);
myApp.controller('myCtrl', function($scope) {
$scope.userid = [1, 2, 3];
$sope.adduser = function() {
}
});
我需要将对象数组发送到我的server.my用户ID也动态它有超过100个数据。如何获得每个数据字段模型的值?以及如何将这些模型数据转换为像我的sampleserver数据一样的对象数组?
我期待我的最终输出数据
var sampleServerData = [{
"userid": 1,
"manualcomment": "mycmt1",
"gender": "male"
}, {
"userid": 2,
"manualcomment": "mycmt2",
"gender": "male"
}, {
"userid": 3,
"manualcomment": "mycmt3",
"gender": "female"
}]
答案 0 :(得分:0)
您应该将textarea ID 更改为类,或在您的用户对象中创建textarea ID(详见下文)。
如果你必须使用的id在一个数组中,那么根据你的id数组创建一个对象数组。
var ids = [1, 2, 3];
var users = ids.map(function(id) {
return {id: id, comment: "", gender: ""};
})
我想我有一个解决方案,我给你一个jsbin。确保按使用js 运行按钮,由于某种原因它不会运行onload。
<div ng-app="myApp">
<div ng-controller="myCtrl">
<form role="form" name='userForm' novalidate>
<div class="container">
<div class="row" ng-repeat="user in users">
<div class="form-group">
<div class="col-md-3">
<label>ID</label>
<input ng-model="user.id" id="user.id" name="user.id" placeholder="Enter bugid" type="text" required readonly disabled>
</div>
<div class="col-md-3">
<label>Comments</label>
<textarea ng-model="user.comment" id="textarea1" rows="1" required></textarea>
</div>
<div class="col-md-3 ">
<label>Gender</label>
<select ng-model="user.gender" name="select2" required>
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</div>
</div>
</div>
</div>
<div class="buttonContainer text-center btn-container">
<br>
<button ng-disabled="userForm.$invalid" type="button" id="adduser" ng-click="adduser()">Add user</button>
<button type="button" class="btn button--default btn--small pull-center">Close</button>
</div>
</form>
</div>
var myApp = angular.module('myApp', []);
myApp.controller('myCtrl', function($scope) {
$scope.ids = [1, 2, 3];
$scope.users = $scope.ids.map(function(id) {
return {id: id, comment: "", gender: ""};
});
$scope.adduser = function() {
var data = $scope.users.map(function(user) {
return {
"userid": user.id,
"manualcomment": user.comment,
"gender": user.gender
}
});
console.log("data", data)
}
});
答案 1 :(得分:0)
在您的HTML文件中,您可以像这样调用ng-repeat;
<div ng-app="myApp">
<div ng-controller="myCtrl">
<form role="form" name='userForm' novalidate>
<div class="container">
<div class="row" ng-repeat="myid in userId">
<div class="form-group">
<div class="col-md-3">
<label>ID</label>
<input ng-model="userId[$index].id" id="myid"name="myid" placeholder="Enter bugid" type="text" required readonly disabled>
</div>
<div class="col-md-3">
<label>Comments</label>
<textarea ng-model="userId[$index].manualcomment" id="textarea1" rows="1" required></textarea>
</div>
<div class="col-md-3 ">
<label>Gender</label>
<select ng-model="userId[$index].gender" name="select2" required>
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</div>
</div>
</div>
</div>
<div class="buttonContainer text-center btn-container">
<br>
<button ng-disabled="userForm.$invalid" type="button" id="adduser" ng-click="addusers()">Add user</button>
<button type="button" class="btn button--default btn--small pull-center">Close</button>
</div>
</form>
</div>
然后在控制器中。
var myApp = angular.module('myApp', []);
myApp.controller('myCtrl', function($scope) {
$scope.userId = [1,2,3,4,5] // or new Array(3);
$sope.adduser = function() {
var serverData = $scope.userId;
}
});
答案 2 :(得分:0)
你可以在ng-repat中使用$ index跟踪,然后使用模型索引
<div class="row" ng-repeat="myid in userid track by $index">
<div class="form-group">
<div class="col-md-3">
<label>ID</label>
<input ng-model="userId[$index].id" id="myid"name="myid" placeholder="Enter bugid" type="text" required readonly disabled>
</div>
<div class="col-md-3">
<label>Comments</label>
<textarea ng-model="userId[$index].manualcomment" id="textarea1" rows="1" required></textarea>
</div>
<div class="col-md-3 ">
<label>Gender</label>
<select ng-model="userId[$index].gender" name="select2" required>
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</div>