I am trying to make a form in angular using json. When I take a single json file it is easy, because I write my html and using iteration I display the form fields. Now if I have 3 or 4 json files I need to generate a different form with different ids and display it. Can I generate dynamic forms?
I take two button which get json data from file "A" and "B". I need to show the different form on click.
plunker
$scope.getFromAFile = function() {
// body...
var inputs=[];
$http.get('a.json')
.success(function (data) {
$scope.formInputs = data.input;
angular.forEach($scope.formInputs, function(value, key) {
/* do something for all key: value pairs */
inputs.push({
"value": value.value,
"inputValues": value.inputValues,
"type": value.inputType.toLowerCase(),
"name": value.name,
"required": value.required
})
});
getFormfromData(inputs,'BID');
})
.error(function(err){
alert(err);
});
}
$scope.getFromBFile = function() {
// body...
$http.get('b.json')
.success(function (data) {
var inputs=[];
$scope.formInputs = data.input;
angular.forEach($scope.formInputs, function (value, key) {
/* do something for all key: value pairs */
inputs.push({
"value": value.value,
"inputValues": value.inputValues,
"type": value.inputType.toLowerCase(),
"name": value.name,
"required": value.required
});
});
getFormfromData(inputs,'BID');
})
.error(function (err) {
alert(err);
});
I am trying to make a form in angular using json. When I take a single json file it is easy, because I write my html and using iteration I display the form fields. Now if I have 3 or 4 json files I need to generate a different form with different ids and display it. Can I generate dynamic forms?
I take two button which get json data from file "A" and "B". I need to show the different form on click.
plunker
$scope.getFromAFile = function() {
// body...
var inputs=[];
$http.get('a.json')
.success(function (data) {
$scope.formInputs = data.input;
angular.forEach($scope.formInputs, function(value, key) {
/* do something for all key: value pairs */
inputs.push({
"value": value.value,
"inputValues": value.inputValues,
"type": value.inputType.toLowerCase(),
"name": value.name,
"required": value.required
})
});
getFormfromData(inputs,'BID');
})
.error(function(err){
alert(err);
});
}
$scope.getFromBFile = function() {
// body...
$http.get('b.json')
.success(function (data) {
var inputs=[];
$scope.formInputs = data.input;
angular.forEach($scope.formInputs, function (value, key) {
/* do something for all key: value pairs */
inputs.push({
"value": value.value,
"inputValues": value.inputValues,
"type": value.inputType.toLowerCase(),
"name": value.name,
"required": value.required
});
});
getFormfromData(inputs,'BID');
})
.error(function (err) {
alert(err);
});
Share
Improve this question
edited Jul 18, 2017 at 8:59
Suraj Rao
29.6k11 gold badges95 silver badges104 bronze badges
asked Aug 11, 2014 at 4:43
user3703527user3703527
2 Answers
Reset to default 6There are good "librairies" to build forms from JSON. To name only 2 of them:
- http://formly-js.github.io/angular-formly/#!/
- https://github./json-schema-form/angular-schema-form
I will suggest you to instead use a library for that, coz why to reinvent the wheel when you already have readymade off shelf ponents..
Use Alpaca JS : http://www.alpacajs/
It allows you creating HTML form using simple JSON structures like this
$(function() {
$("#form1").alpaca({
"schema": {
"title": "User Feedback",
"description": "What do you think about Alpaca?",
"type": "object",
"properties": {
"name": {
"type": "string",
"title": "Name"
},
"ranking": {
"type": "string",
"title": "Ranking",
"enum": ['excellent', 'not too shabby', 'alpaca built my hotrod']
}
}
}
});
});
And the output for this will be something like