0

I would just like to know how to create this from javascript using angularJS:

<input type="text" ng-model="TheText">{{TheText}}

For example using something like this:

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body ng-app="myApp" ng-controller="myCtrl">
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.7/angular.min.js"></script>
        <div id="Holder"></div>

        <script>
            angular.module('myApp', [])
            .controller('myCtrl', function() {
                var newdiv = document.createElement('div');
                newdiv.innerHTML = '<input type="text" ng-model="TheText">{{TheText}}';
                document.getElementById("Holder").appendChild(newdiv);
            });
        </script>
    </body>
</html>

I assume you would have to use $compile or something like that but I don't seem to get any of that working. Basically the problem is that any ng directives dont't see to work when I create it this way.

SOLUTION:

Use custom directives, this answer solves my problem:

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body ng-app="myApp">
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.7/angular.min.js"></script>
        <div mydiv></div>

        <script>
            angular.module("myApp", [])
            .directive("mydiv", function() {
                return {
                    template: '<input type="text" ng-model="TheText">{{TheText}}'
                };
            });
        </script>
    </body>
</html>

1 Answer 1

1

Why not to try simple (but powerful) html() method:

iElement.html('<svg width="600" height="100" class="svg"></svg>');

Or append as an alternative:

iElement.append('<svg width="600" height="100" class="svg"></svg>');

And , of course , more cleaner way:

 var elt = angular.element('<input type="text" ng-model="TheText">{{TheText}}');
 iElement.append(elt);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Leo, would you might explaining the answer a bit more please? I don't understand where iElement comes from

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.