使用json模式和angularjs动态生成表单
我有一个json模式,如下所示:使用json模式和angularjs动态生成表单,angularjs,json,angularjs-directive,Angularjs,Json,Angularjs Directive,我有一个json模式,如下所示: var schema = { "type": "object", "properties": { "requestedFor": { "type": "string" }, "location": { "type": "string" }, "shortDescription":{ "type":
var schema = {
"type": "object",
"properties": {
"requestedFor": {
"type": "string"
},
"location": {
"type": "string"
},
"shortDescription":{
"type": "string"
},
"description": {
"type": "string"
}
}
};
var options = {
"fields": {
"requestedFor": {
"requestedFor": "text",
"label": "*Name"
},
"location": {
"type": "text",
"label": "*Location"
},
"shortDescription":{
"type": "text",
"label": "Short Description"
},
"description": {
"type": "textarea",
"label": "Description",
"rows": 5,
"cols": 60,
"label": "",
"wordlimit": 250
}
},
"form": {
"attributes": {
"method": "POST",
"action": "#",
"enctype": "multipart/form-data"
},
"buttons": {
"submit": {
"value": "Submit"
}
}
}
};
var app = angular.module("app", [])
.directive("wrapper", function($compile, $interval) {
return {
scope: {
directiveName: "="
},
link: function(scope, elem, attrs) {
scope.$watch("directiveName", function() {
var html = "<" + scope.directiveName + "></" + scope.directiveName + ">";
elem.html(html);
$compile(elem.contents())(scope);
});
}
}
}).controller("addDirectives", function($scope, $interval) {
let directiveNames = ['d1', 'd2', 'd3', "d4"];
$scope.directiveName = directiveNames[0];
}).directive("d1", function() {
return {
template: "<input type='text'>Requested For</input>"
}
}).directive("d2", function() {
return {
template: "<input type='text'>Location</input>"
}
}).directive("d3", function() {
return {
template: "<input type='text'>Short Description</input>"
}
}).directive("d4", function(){
return {
template: "<input type='textarea'>Description</input>"
}
})
因此,我想使用这个json模式使用angularjs动态创建一个自定义指令。因此,我创建了如下代码:
var schema = {
"type": "object",
"properties": {
"requestedFor": {
"type": "string"
},
"location": {
"type": "string"
},
"shortDescription":{
"type": "string"
},
"description": {
"type": "string"
}
}
};
var options = {
"fields": {
"requestedFor": {
"requestedFor": "text",
"label": "*Name"
},
"location": {
"type": "text",
"label": "*Location"
},
"shortDescription":{
"type": "text",
"label": "Short Description"
},
"description": {
"type": "textarea",
"label": "Description",
"rows": 5,
"cols": 60,
"label": "",
"wordlimit": 250
}
},
"form": {
"attributes": {
"method": "POST",
"action": "#",
"enctype": "multipart/form-data"
},
"buttons": {
"submit": {
"value": "Submit"
}
}
}
};
var app = angular.module("app", [])
.directive("wrapper", function($compile, $interval) {
return {
scope: {
directiveName: "="
},
link: function(scope, elem, attrs) {
scope.$watch("directiveName", function() {
var html = "<" + scope.directiveName + "></" + scope.directiveName + ">";
elem.html(html);
$compile(elem.contents())(scope);
});
}
}
}).controller("addDirectives", function($scope, $interval) {
let directiveNames = ['d1', 'd2', 'd3', "d4"];
$scope.directiveName = directiveNames[0];
}).directive("d1", function() {
return {
template: "<input type='text'>Requested For</input>"
}
}).directive("d2", function() {
return {
template: "<input type='text'>Location</input>"
}
}).directive("d3", function() {
return {
template: "<input type='text'>Short Description</input>"
}
}).directive("d4", function(){
return {
template: "<input type='textarea'>Description</input>"
}
})
var-app=angular.module(“app”,[])
.directive(“包装器”,函数($compile,$interval){
返回{
范围:{
directiveName:“”
},
链接:功能(范围、要素、属性){
作用域.$watch(“directiveName”,function()){
var html=“”;
elem.html(html);
$compile(elem.contents())(范围);
});
}
}
}).controller(“addDirectives”,函数($scope,$interval){
设directiveNames=['d1','d2','d3','d4];
$scope.directiveName=directiveNames[0];
}).指令(“d1”,函数(){
返回{
模板:“请求用于”
}
}).指令(“d2”,函数(){
返回{
模板:“位置”
}
}).指令(“d3”,函数(){
返回{
模板:“简短描述”
}
}).指令(“d4”,函数(){
返回{
模板:“说明”
}
})
这里我的意图是使用这个json模式动态生成表单,因为我有太多的json文件可用,所以我需要创建不同的自定义指令
<!DOCTYPE html>
<html ng-app="app">
<head>
<body ng-controller="addDirectives">
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="form"></div>
</div>
</div>
</div>
</body>
</html>
那么,有人能提供解决方案吗?看看angular formly和其他已经存在的动态表单模块,我正在使用alpacajs库实现json模式。羊驼不可能吗?