AngularJS dynamic forms, how to place schema component in the right place in html
I have created the following schema using angular schemaform. But I want the name to go to div1 and the age to go to div2. Please help me how to do this.
$scope.schema = {
type: "object",
properties: {
"name": {
"type": "string",
"title": "Name",
"required": true
},
"age": {
"type": "number",
"title": "Age"
}
}
};
$scope.form = [
"*",
{
type: "submit",
title: "Save"
}
];
<div id="div1"></div>
<div id="div2"></div>
source to share
I am one of the guys working on the ASF project. There are 3 ways to go here.
-
As mentioned above, one way is to create custom templates (or decorators, as we call them).
-
The second uses the experimental feature to insert a field manually. (may work well)
-
The third way is a little more hackish, but fast. That is, launch multiple instances of ASF with the same schema and model, but split the form definition.
JS (simplified):
$scope.schema = {
"type": "object",
"properties": {
"name": {
"type": "string",
"title": "Name",
"required": true
},
"age": {
"type": "number",
"title": "Age"
}
}
};
$scope.formOne = [
"name"
];
$scope.formTwo = [
"age"
];
$scope.model = {};
And the html will be something like this:
<div id="div1">
<form sf-schema="schema" sf-form="formOne" sf-model="model"></form>
</div>
<div id="div2">
<form sf-schema="schema" sf-form="formTwo" sf-model="model"></form>
</div>
source to share
Play with this plunger snippet: http://plnkr.co/edit/mpZrVO?p=preview
The key line here is the tag <body>
index.html
: <form sf-schema="schema" sf-form="form" sf-model="model"></form>
. Your best bet is to style each outline / shape as one unit to maintain a consistent interface rather than two divs.
Make sure to include the correct dependencies in your module.
This is what looks like app.js
:
var app = angular.module('plunker', ['ngSanitize', 'schemaForm']);
app.controller('MainCtrl', function($scope) {
$scope.schema = {
"type": "object",
"properties": {
"name": {
"type": "string",
"title": "Name",
"required": true
},
"age": {
"type": "number",
"title": "Age"
}
}
};
$scope.form = [
"*", {
type: "submit",
title: "save"
}
];
$scope.model = {};
});
source to share