0

I try to make dynamic form, that will be created from server and sended to client. In server I create this code:

    <div ng-app="myapp">
        <div>

        <form ng-controller="MyController"><br>
            <input id="desc" pattern="[A-Z][a-z 0-9]*" required="" type="text" class="InputDoc" ng-model="Document.desc"><br>
            <input id="number" min="4" max="40" required="" type="number" class="InputDoc" ng-model="Document.number"><br>
        </form>

        </div>      
    </div>

And client have only div tag as place, where I want to store output:

<div id="result">
        </div>

But in JavaScript file, when data was downloaded from XHR function, I create element div and script:

var div = document.createElement("div");
    div.setAttribute("ng-app", "myapp");
    var script = document.createElement("script");
    div.innerHTML = res;
    script.innerHTML = "angular.module('myapp', []).controller('MyController', function($scope){    $scope.Document = {};   $scope.Document.title = 'test1'; $scope.Document.desc = 'test3';    });";
    div.appendChild(script);
    document.getElementById("result").appendChild(div);

After this, my page is rendered exacly, how I imagine, but input types, and controller are not binding, and values aren't set. But, if I copy rendered content, and save it as other page and run, then everything works and is fine.

How to dynamically set that form ? I should have some dynamic bindings, because sometimes these inputs will be 5, sometimes 10.

InnerWorld
  • 585
  • 7
  • 26
  • you need to manual boot angularjs https://docs.angularjs.org/guide/bootstrap i guess – itsme Dec 22 '14 at 20:44
  • Here is how to load and compile scripts - should be nearly what u want http://stackoverflow.com/questions/18157305/angularjs-compiling-dynamic-html-strings-from-database But reconsider one more time - do u really need this or not... – Petr Averyanov Dec 22 '14 at 21:20
  • angular doesn't care how many inputs , set the main model object all the same and it will automatically create all the bound properties for you – charlietfl Dec 23 '14 at 02:21
  • sbaaaang - Yes, it works how I imagine this. – InnerWorld Dec 23 '14 at 07:59
  • Petr Averyanov - for compiling html I should make more changes in code. At this moment I learn AngularJS and I can try to create new document with this option, but why you think, that I should not need this ? Is it bad solution for dynamic content ? – InnerWorld Dec 23 '14 at 08:03

0 Answers0