I'm trying to store data from form created dynamiclly at runtime.
Please take under consideration that I'm hardcore noob at .js
I got the idea to append HTML with class like this:
<div class="formElement"> some code here </div>
... each time user clicks a buton.
It's all great, but I'm stuck on getting values using angular/jquery from each of those divs and then parsing them into json format that would match viewmodel class. Can you help me with getting all of them?
The types in each div are:
- select list
- number input
- text input
html code
<div ng-app="formExample" ng-controller="ExampleController">
<button class="btn btn-primary" ng-controller="addRow" ng-click="addLine()">Dodaj przycisk</button>
<form novalidate class="simple-form">
<div class="here">
<div class="formElement">
Klasa:<select class="optns" ng-model="user.class">
<option selected value="1"> Rodzaj... </option>
<option value="2">2</option>
@*tutaj beda dodane opcje*@
</select>
Ilość: <input type="number" ng-model="user.number" value=""/><br />
Uwagi: <input type="text" ng-model="user.text" value=""/><br />
</div>
</div>
<input type="button" ng-click="reset()" value="Reset" />
<input type="submit" ng-controller="addRow" ng-click="getValues()" value="Save" />
</form>
<pre>user = {{user | json}}</pre>
<pre>master = {{master | json}}</pre>
</div>
angular code
var app = angular.module('formExample', []);
app.controller('ExampleController', ['$scope', function ($scope) {
$scope.master = {};
$scope.nrofrows = 0;
$scope.update = function(user) {
$scope.master = angular.copy(user);
$scope.another = angular.copy()
};
$scope.reset = function() {
$scope.user = angular.copy($scope.master);
};
$scope.reset();
}]);
app.controller('addRow', function ($scope) {
$scope.getValues = function () {
var allInputs = $(':input');
@* przy selected ewentualnie jeszcze zamiania :select na klase/id*@
var selected = $(".optns option:selected").each(function ($scope) {
$scope.arraySels.push($(this));
});
var quantities = $('.formElement :input').attr['number'].each(function () {
$scope.arrayQuants.push($(this));
});
var texts = $('.formElement :input').attr['text'].each(function () {
$scope.arrayTexts.push($(this));
});
alert("works" + $scope.arrayQuants);
};