1

I want to use this library (angular-dynamic-forms) to generate dynamic form from my JSON data

So this is my HTML file

<!doctype html>
<html ng-app="app" id="ng-app">
    <head>
        <title>Hello form</title>
    </head>

    <body ng-controller="AppCtrl" ng-cloak="">
        <dynamic-form class="col-md-12" 
            template="stdFormTemplate" 
            ng-model="stdFormData">
        </dynamic-form>

        <script src="./libs/angularjs/1.0.8/angular.min.js"></script>
        <script src="./libs/components/dynamic-forms.js"></script>
        <script src="./controller/dynamic-forms.js"></script>
    </body>
</html>

Now if I have a controller like this everything works fine, I have my form

(function(){
    var app = angular.module('app', ['dynform']);
    app.controller('AppCtrl', function ($http, $scope) {
        $scope.stdFormTemplate = [
            {
                "type": "text",
                "model": "name.first",
                "label": "Last Name",
            },
            {
                "type": "text",
                "label": "Last Name",
                "model": "name.last",
            },
            {
                "type": "email",
                "label": "Email Address",
                "model": "email",
            },
            {
                "type": "submit",
                "label": "submit",
                "model": "submit",
            }
        ];

        $scope.stdFormData = {
            "name": {
                "first": "Saman",
                "last": "Shafigh"
                },
            "email": "[email protected]"
        };            
    });
})();

But when I want to use dynamic JSON data then it does not work. For example I have an API service which returns JSON content like this

{"form":{
     "template":[
          {"type":"text","model":"title","label":"","required":true},
          {"type":"textarea","model":"description","label":"","required":true},
          {"type":"number","model":"price","label":"","required":true},
          {"type":"choice","model":"available","label":"","required":true}],
     "data":{"title":"","description":"","price":"","available":"1"}}
 }

Then I want to use this JSON to generate my form dynamically, my code is like this

(function(){
    var app = angular.module('app', ['dynform']);

    app.controller('AppCtrl', function ($http, $scope) {
        $http.get('../api/product/form').
            success(function(data) {
                $scope.stdFormData = data.form.data;
                $scope.stdFormTemplate = data.form.template;
            });   
    });
})();

Even the following code is not working (it is just for test).

(function(){
    var app = angular.module('app', ['dynform']);
    app.controller('AppCtrl', function ($http, $scope) {
        $http.get('../api/product/form').
            success(function(data) {
                $scope.stdFormTemplate = [
                    {
                        "type": "text",
                        "model": "name.first",
                        "label": "Last Name"
                    },
                    {
                        "type": "text",
                        "label": "Last Name",
                        "model": "name.last"
                    },
                    {
                        "type": "email",
                        "label": "Email Address",
                        "model": "email"
                    },
                    {
                        "type": "submit",
                        "label": "submit",
                        "model": "submit"
                    }
                ];

                $scope.stdFormData = {
                    "name": {
                        "first": "Saman",
                        "last": "Shafigh"
                        },
                    "email": "[email protected]"
                };            

        });   
    });
})();

I think the form is not initialized when I initialize it in an action that has some delay.

2
  • How to work out in the end? Commented Feb 17, 2016 at 9:11
  • Hi @user1882196 I have posted my solution Commented Feb 17, 2016 at 23:36

1 Answer 1

1

I try something like this and it works for me, I've confirmed it with #danhunsaker who is the main contributions of this module, that for the moment this is the best solution.

In my html I have something like

<div id="my-form"></div>

Then in controller I have

(function(){
    var app = angular.module('app', ['dynform']);

    app.controller('AppCtrl', function ($http, $compile, $scope) {
        $http.get('../api/product/form/1').
            success(function(data) {
                $scope.stdFormData = data.form.data;
                $scope.stdFormTemplate = data.form.template;

                var element = angular.element(document.getElementById("my-form"));
                element.html('<dynamic-form class="col-md-12" template="stdFormTemplate" ng-model="stdFormData"></dynamic-form>');
                $compile(element.contents())($scope);                
            });   
    });
})();
Sign up to request clarification or add additional context in comments.

1 Comment

I've confirmed it with #danhunsaker who is the main contributions of this module, that for the moment this is the best solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.